python - Does multiple assignment make may code slower? -


my question simple one. using multiple assignments make code slower ? (even if slower, worth considering ? )

i need performance code. assignment in loop possibly executed many times.

i made quick check of things. , not sure how analyse these results or conclusion should derive them. let me put here you.

import timeit print(timeit.timeit("a = 1; b = 2", number=100000)) print(timeit.timeit("a = 1; b = 2", number=100000)) print(timeit.timeit("a = 1; b = 2", number=100000))  print  print(timeit.timeit("a, b = (1, 2)", number=100000)) print(timeit.timeit("a, b = (1, 2)", number=100000)) print(timeit.timeit("a, b = (1, 2)", number=100000))  print  print(timeit.timeit("a = (1, 2) ;b =a[0] ;c = a[1]", number=100000)) print(timeit.timeit("a = (1, 2) ;b =a[0] ;c = a[1] ", number=100000)) print(timeit.timeit("a = (1, 2) ;b =a[0] ;c = a[1] ", number=100000))  

gives:

0.00271645813545 0.00316250124554 0.00289307923274  0.00323691303956 0.00329421867402 0.00343021264239  0.011346943279 0.0154434408356 0.0141857104552 

i ran several times , overall result.

there appears slight difference (except creating tuple first, obvious, wanted see.), should conclude this?

this specific question answered along discussion points here: https://wiki.python.org/moin/pythonspeed (they assert yes, multiple assignment slower, except when used swap.)

i eliminated creation of tuple running this:

import timeit print (timeit.timeit("i=(1,2); a=1; b=2", number=1000000)) print (timeit.timeit("i=(1,2); a, b = i", number=1000000)) 

the difference seemed more here suggest, when reverse order timed, first timeit seemed run slower. slow enough took longer assign twice, rather multiple assign tuple. conclude testing mechanism flawed.

multiple assignment slower, if you're doing multiple assignments in loop, can add up. have doing lot, , suggests there better way. improve code's performance, 1) profile , figure out taking long. 2) analyze algorithm find things better.

the profiler may agree , loop slow, when human tries guess what's slow ... wrong. , waste days trying fix that's not slowing down code, , end no change.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -