python - Tuple comprehension time profiling -


lately i've been doing time profiling of scripts. wondering tuple comprehension i've found several threads on pointing out 2 ways of doing :

  • list comprehension + tuple()

    >>> tuple([i in xrange(1000000)]) 
  • tuple comprehension

    >>> tuple(i in xrange(1000000)) 

i'm puzzled fact cprolile , timeit tell me first method faster second on , command line time , kernprof line profiler contrary.

here get:

>>> import cprofile >>> cprofile.run('tuple([i in xrange(1000000)])') 1000003 function calls in 0.139 seconds >>> cprofile.run('tuple(i in xrange(1000000))') 1000003 function calls in 0.478 seconds   >>> import timeit >>> timeit.timeit('tuple([i in xrange(1000000)])') 0.08100390434265137 >>> timeit.timeit('tuple(i in xrange(1000000))') 0.08400511741638184 

with test_tuple_list.py:

tuple([i in xrange(1000000)]) 

and test_tuple_generator.py:

tuple(i in xrange(1000000)) 

i get:

$time python test_tuple_list.py real 0m0.398s user 0m0.171s sys 0m0.202s  $time python test_tuple_generator.py real 0m0.333s user 0m0.109s sys 0m0.234s 

with test_tuple_list_kernprof.py

@profile def test():     tuple([i in xrange(1000000)]) test() 

and test_tuple_generator_kernprof.py:

@profile def test():    tuple(i in xrange(1000000)) test() 

i get:

$kernprof.py -lv test_tuple_list_kernprof.py total time: 0.861045 s  $kernprof.py -lv test_tuple_generator_kernprof.py total time: 0.444025 s 

i assume difference between these profiler comes way profile, how comes contradict 1 ?

thank you

do not use profiler measure overall timing differences between 2 snippets of python code. profile severely impacts code execution times across interpreter, different code paths trigger sys.set_trace() trace function @ different times, , trace function introduce subtle timing differences different events skew results making data useless absolute timing comparisons.

when profiling, measuring how the profiler reacts different code paths measuring code paths themselves. that's fine when want pinpoint where in complex code execution time goes, terrible comparing 2 different pieces of code purely on how fast perform.

that leaves just timeit results, close call. both methods fast 1 another.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -