i tested performance for in
loop in python. contains loop , plus operations. takes 0.5secs. how can more faster?
import time start_time = time.time() val = -1000000 in range(2000000): val += 1 elapsed_time = time.time() - start_time print(elapsed_time) # 0.46402716636657715
the way faster using itertools
module written in c gain c performance:
example:
from itertools import repeat def foo(): val = -1000000 in range(2000000): val += 1 return val def bar(): val = -1000000 return val + sum(repeat(1, 2000000))
results:
$ python -m timeit -s "from foo import foo" "foo()" 10 loops, best of 3: 102 msec per loop prologic@daisy wed jun 03 19:51:54 ~/tmp $ python -m timeit -s "from foo import bar" "bar()" 100 loops, best of 3: 10.5 msec per loop
but have t8 wonder why summing val += 1 in loop 2,000,000 times?
the optimal solution (of course) is:
val = 100000
Comments
Post a Comment