Should I optimize my Python code, e.g. C ++? Does it matter?

I had an argument with a colleague in writing python effectively. He argued that while you are programming python, you still need to optimize the small fragments of your software as much as possible, as if you were writing an efficient algorithm in C ++.

Things like:

  • The operator ifc oralways sets a condition with the probability of a failure first, so the second will not be verified.
  • Use the most efficient functions to easily use strings. Not code that cuts strings, but simple things like concatenating and splitting, and finding substrings.
  • Call as few functions as possible, even if this is due to readability, due to the overhead that it creates.

I say that in most cases it does not matter. I must also say that the code context is not - it is super-efficient NOC or missile control systems. We mainly write tests in python.

How do you feel about this issue?

+5
source share
10 answers

My answer to this question:

We should forget about small efficiencies, say, about 97% of the time: premature optimization is the root of all evil.

(Quoting Knuth, Donald. Structured Programming with Transition to Claims, ACM Journal Computing Surveys, Vol. 6, No. 4, December 1974. p. 268)


- , , , , ...

, , ? Python / ? , , : -)

, ; CPU-time!
, , , , ?

+14

:

  • Python, ++.
  • Python , .
  • , , .
+13

- , C ++. . , , , .

, . "" , ? .

( , . - .)

+10

: ( " , ".).

, , / , , . , , , .

+4

, NOC . python.

, , Python, , , , , , , , .

+3

if , , -, .

, , , . , , false, . .

. , , , .

. , , , , , C, python, , . .

, - .

$ cat withcall.py
def square(a):
        return a*a

for i in xrange(1,100000):
        i_square = square(i)

$ cat withoutcall.py
for i in xrange(1,100000):
        i_square = i*i

$ time python2.3 withcall.py
real    0m5.769s
user    0m4.304s
sys     0m0.215s
$ time python2.3 withcall.py
real    0m5.884s
user    0m4.315s
sys     0m0.206s

$ time python2.3 withoutcall.py
real    0m5.806s
user    0m4.172s
sys     0m0.209s
$ time python2.3 withoutcall.py
real    0m5.613s
user    0m4.171s
sys     0m0.216s

... ... .

+2

, " ".

  • False , .

  • True , , Python , .

  • True. Python, , Python.

: , , , , . ''.join(), , ( s += x - ).

" " Pythonic- . , Python.

+2

, , psyco, JIT-ish , , .

, , . - . hotspot , .

, :

def get_order_qty(ordernumber):
    # look up order in database and return quantity

, , @memoize, . memoizing , , , , , .

, . - , , , - .

(, , ? • if , .

, "", "" , "", . :

  • "A B", A, .
  • "A B", A, , , .

:

if obj is not None and hasattr(obj,"name") and obj.name.startswith("X"):

- ( :

if obj.name.startswith("X"):
+2

, Python ( ), , ( ).

" IF ", , , , , , . ( ) .

If after measuring, profile, or in my case do it , you really know that you can save a lot of time by re-ordering the tests, by all means, do it. My money says it is 1% or less.

+1
source

My visceral reaction is as follows:

I worked with the guys, like your colleague, and in general I would not consult with them.

Ask him if he ever used a profiler.

+1
source

All Articles