Skip to content Skip to sidebar Skip to footer

Timing Functions

Warning, this is a bit recursive ;) I answered this question: Python:How can i get all the elements in a list before the longest element? And after I submitted there where another

Solution 1:

The lambda is costing dearer in the second solution.

I profiled both the codes and by the profile data, it looks, the first solution is faster

As the wiki would say function call is costly and in the second solution, the lambda and the len function calls are making it run slower

Please note, I have trimmed down the list to a length of 1000 elements

>>> cProfile.run('solution1(lst)')
         5functioncallsin 0.000 CPUsecondsOrderedby: standardnamencallstottimepercallcumtimepercallfilename:lineno(function)10.0000.0000.0000.000 <pyshell#305>:1(solution1)
        10.0000.0000.0000.000 <string>:1(<module>)
        10.0000.0000.0000.000 {max}
        10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects}
        10.0000.0000.0000.000 {method 'index' of 'list' objects}


>>> cProfile.run('solution2(lst)')
         2004functioncallsin 0.012 CPUsecondsOrderedby: standardnamencallstottimepercallcumtimepercallfilename:lineno(function)10.0000.0000.0120.012 <pyshell#306>:1(solution2)
     10000.0060.0000.0090.000 <pyshell#306>:2(<lambda>)
        10.0000.0000.0120.012 <string>:1(<module>)
     10000.0030.0000.0030.000 {len}
        10.0030.0030.0120.012 {max}
        10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects}

Solution 2:

It seems first version is making many less calls than the second one.

btw, This is probably another example of how idiomatic, simple code is often also the faster one in Python

>>> dis.dis(solution1)
  20 LOAD_FAST                0 (lst)
              3 LOAD_FAST                0 (lst)
              6 LOAD_ATTR                0 (index)
              9 LOAD_GLOBAL              1 (max)
             12 LOAD_FAST                0 (lst)
             15 LOAD_CONST               1 ('key')
             18 LOAD_GLOBAL              2 (len)
             21 CALL_FUNCTION          25724 CALL_FUNCTION            127 SLICE+228 RETURN_VALUE        
>>> dis.dis(solution2)
  20 LOAD_GLOBAL              0 (max)
              3 LOAD_GLOBAL              1 (enumerate)
              6 LOAD_FAST                0 (lst)
              9 CALL_FUNCTION            112 LOAD_CONST               1 ('key')
             15 LOAD_CONST               2 (<code object <lambda> at 000000000422BEB8, file "<input>", line 2>)
             18 MAKE_FUNCTION            021 CALL_FUNCTION          25724 UNPACK_SEQUENCE          227 STORE_FAST               1 (idx)
             30 STORE_FAST               2 (maxLenStr)

  333 LOAD_FAST                0 (lst)
             36 LOAD_FAST                1 (idx)
             39 SLICE+240 RETURN_VALUE 

Solution 3:

The timing looks ok.

solution1 can be faster, because it doesn't use lambdas, so it doesn't need to call Python code in the loop. It does iterate the list twice, but it's not a big deal.

Post a Comment for "Timing Functions"