diff --git a/boilerplates-optimization/README.md b/boilerplates-optimization/README.md new file mode 100644 index 0000000..945afde --- /dev/null +++ b/boilerplates-optimization/README.md @@ -0,0 +1,69 @@ +## Notes on optimization in Python + +
+ +### Peephole optimization + +* optimization technique done at the compile time to improve code performance +* code is optimized behind the scenes and it's done either by pre-calculating constant expressions or using membership tests +* turn mutable constructs into immutable constructes +* turn both set and lists into constants + +
+ +### Intern strings + +* string objects in Python are sequences of Unicode characters, called text sequences +* when characters of different sizes are added to a string, its total size and weight increase, but not only by the size of the added character. Python also allocates extra information to store strings, which causes them to take up too much space. +* The idea behund string interning is to cache certain strings in memory as they are created, which has lot in common with shared objects. For instance, CPython loads shared objects into memory every time a Python interactive session is initialized. + +
+ +### Profiling code + +* use timeit +* use cProfile: advanced profiling, generates reports + + +
+ +### Use generators and keys for soring + +- Generators dont' return ites such as iterators, but only one item at time. + + +
+ +### Use "C" equivalent of some Python libs + +* they are the same features but with faster performance. Example: instead of Pickle, use cPickle, etc. +* Use PyPy package and Cytjon to optimiza a static compiler. + + +
+ + +### Avoid using Globals + +* Ugh for spaghetti code... +* Or make a local copy before using them inside loops. + + +
+ +### Use technology stacks + +* redis for cache +* rabbitmq or celert for job queues and exports + + + + + + + + +* + + +