Reputation:
Why is my code so sluggish (inefficient)? I need to make two methods to record the time it takes to process a list of a given size. I have a search_fast
and search_slow
method. Even though there is a difference between those two search times. Search_fast
is still pretty slow. I'd like to optimise the processing time so instead of getting 8.99038815498
with search_fast
and 65.0739619732
with search_slow
. It would only take a fraction of a second. What can I do? I'd be eternally grateful for some tips as coding is still pretty new to me. :)
from timeit import Timer
def fillList(l, n):
l.extend(range(1, n + 1))
l = []
fillList(l, 100)
def search_fast(l):
for item in l:
if item == 10:
return True
return False
def search_slow(l):
return_value = False
for item in l:
if item == 10:
return_value = True
return return_value
t = Timer(lambda: search_fast(l))
print t.timeit()
t = Timer(lambda: search_slow(l))
print t.timeit()
Upvotes: 3
Views: 209
Reputation:
I managed to find out what made the code sluggish. It was a simple mistake of adding to the list byextend
instead of append
.
def fillList(l, n):
l.**append**(range(1, n + 1))
l = []
fillList(l, 100)
Now search_slow
clocks in at 3.91826605797
instead of 65.0739619732
. But I have no idea why it changes the performance so much.
Upvotes: 0
Reputation: 3965
Adding the following code to above:
t = Timer(lambda: 10 in l)
print(t.timeit())
produces the following on my system:
0.6166538814701169
3.884095008084452
0.29087270299795875
>>>
Hope this helps. The basic idea is to tap into underlying C code and not make your own Python code.
Upvotes: 1
Reputation: 4758
The fastest way is using in
operator, which tests membership of a value in a sequence.
if value in some_container:
…
Reference: https://docs.python.org/3/reference/expressions.html#membership-test-operations
Update: also, if you frequently need to test the membership, consider using sets instead of lists.
Some pros and cons can be found here: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
Upvotes: 1