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...
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
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
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.