Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing functions

Tags:

python

time

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 answer that should be faster (the author thought, and so did I). I tried to time the different solutions but the solution that should be slower was actually faster. This made me think there is something wrong with my code. Or is it?

import string
import random
import time

def solution1(lst):
  return lst[:lst.index(max(lst, key=len))]

def solution2(lst):
  idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))
  return lst[:idx]

# Create a 100000 elements long list that contains
# random data and random element length
lst = []
for i in range(100000):
  s = "".join([random.choice(string.letters+string.digits) for x in range(1, random.randint(1,50))])
  lst.append(s)

# Time the first solution
start = time.time()
solution1(lst)
print 'Time for solution1', (time.time() - start)

# Time the second solution
start = time.time()
solution2(lst)
print 'Time for solution2', (time.time() - start)

Update

Before anyone mentions why I put this is as a new question. The question is more about me learning how to measure execution time...

like image 230
Niclas Nilsson Avatar asked Jan 05 '12 11:01

Niclas Nilsson


People also ask

What is default animation-timing-function?

Defines how the values between the start and the end of the animation are calculated. default animation-timing-function: ease; The animation starts slowly, accelerates in the middle, and slows down at the end.

What are easing functions?

An easing function just defines a transition between a start and end values. Those could be x coordinates, or a color, or the transparency of an object. And in fact, in theory, you could apply different easing function to interpolate for different properties. Hopefully this helps shed some light on the basic idea.

What is animation timing?

Timing animation refers to how long an action takes from beginning to end. The functions of timing are to create movement that obeys the laws of physics, and to add interest to your animations. Timing can be implemented by applying weight, scaling properties, and emotion.

What does transition-timing-function do in CSS?

CSS Demo: transition-timing-function This, in essence, lets you establish an acceleration curve so that the speed of the transition can vary over its duration. This acceleration curve is defined using one <easing-function> for each property to be transitioned.


1 Answers

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)')
         5 function calls in 0.000 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <pyshell#305>:1(solution1)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}


>>> cProfile.run('solution2(lst)')
         2004 function calls in 0.012 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.012    0.012 <pyshell#306>:1(solution2)
     1000    0.006    0.000    0.009    0.000 <pyshell#306>:2(<lambda>)
        1    0.000    0.000    0.012    0.012 <string>:1(<module>)
     1000    0.003    0.000    0.003    0.000 {len}
        1    0.003    0.003    0.012    0.012 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
like image 200
Abhijit Avatar answered Sep 29 '22 13:09

Abhijit