Skip to content

Latest commit

 

History

History
95 lines (65 loc) · 3.4 KB

File metadata and controls

95 lines (65 loc) · 3.4 KB

Tips for writing faster python ( CPython) :


Note : Always profile and benchmark your code first.


  1. Always prefer use builtin functions, because most of the time they are way too faster as being implemented in C. (for cpython)

  2. Use sets instead of lists, as it provides much faster lookups.

  3. Code snippet :

for i in range(100):
    doSomethingWithX()    # Much slower

map(doSomethingWithX() ,  xrange(0,100))        # Much Faster , because interpreter only have to resolve the function name once.
  1. try'ing is cheap, if'ing is expensive , when the chances of getting into the exception are less (execution inside except block is slower).
# Slower
if (type(a) == str):
  a = int(a)

# Faster : python eafp https:#stackoverflow.com/questions/11360858/what-is-the-eafp-principle-in-python
try: 
  a  = int(a)
except:
  pass
  1. For generating a long string ( 'str_generated' ) in python :
# Slower
str_generated = ""
for x in list:
    str_generated += some_function(x)

# Faster
slist = [some_function(x) for x in somelist]
str_generated = "".join( slist )

Reason :  python strings are immutable so everytime you do '+=' it everytime creates new string.
  1. Use generators over iterators.(It is memory efficient, but doesn't effect the speed of execution)

  2. Global import statements are faster than local import statements ( import written inside a function), this is the behaviour of python interpreter.

  3. Function call overhead in Python is relatively high, (especially compared with the execution speed of a builtin function), This strongly suggests that wherever appropriate, functions should handle data aggregates.

  4. How to find double of a number (say 'num') ?

a. num = num + num
b. num = num*2
c. num = num<<2

Expected performance : a (worst) < b < c (Best, as it's a bit shift)
Observed performance : a (Best)
The reason we expect 'c' to be best is because the compiler(in C++,java etc) converts it into 1 machine instruction, whereas python doesn't have that concept.
  1. For infinite loop while 1 is better than while True the reason being that while 1 is converted into a single jump operation , this is in python2 only , for python3 it's the same. The reason for this being in python2 True is not listed as a keyword, so everytime the interpreter has to fetch it's value and the evaluate whereas in python3 True is listed as a keyword and thus it doesn't have to fetch everytime.

  2. List comprehensions are better than for loop

  3. Avoid global variables as much as possible (not a good pratice too) , python lookup in global space is too time consuming as compared to local variables.

  4. Avoid unwanted loops (shouldn't be saying this though)

For finding common element in 2 lists 'a' and 'b' : 
for x in a:
    for y in b:
        if x == y:
            c.append(x)
return c

return set(a) & set(b) # Obv this is faster
  1. Keep your python code small and tight, don't forget python is interpreted after-all.

  2. Use numpy incase of arrays mathematical computation etc. after all it's in cython and thus much faster. For more speed port to cython.

Resources/References :