All in all my results are not the same. And I have struck upon something interesting! All I am wondering now is why.
Screen Link:
https://app.dataquest.io/m/481/guided-project%3A-building-fast-queries-on-a-csv/5/comparing-the-performance
My Code:
def get_laptop_from_id(self, laptop_id):
for row in self.rows:
if row[0] == laptop_id:
return row
else:
return None
def get_laptop_from_id_fast(self, laptop_id):
if laptop_id in self.id_to_row:
return self.id_to_row[laptop_id]
else:
return None
The solution code:
def get_laptop_from_id(self, laptop_id):
for row in self.rows:
if row[0] == laptop_id:
return row
return None #Note the difference here, I have an else: statement.
def get_laptop_from_id_fast(self, laptop_id):
if laptop_id in self.id_to_row:
return self.id_to_row[laptop_id]
return None #Note the difference here, I have an else: statement.
After this we time the difference between the two class-methods and we should see that the fast method is faster than the no_dictionary method because a dictionary has a direct lookup structure.
What I expected to happen:
print(total_time_no_dict) # step 9
print(total_time_dict)
0.5494911670684814
0.002789735794067383
As you can see dict would be a lot faster in the solutions.
What actually happened when using my method (with if else
):
print(total_time_no_dict)
print(total_time_dict)
0.006408214569091797
0.010406017303466797
Using the functions from the solution notebook I got:
print(total_time_no_dict)
print(total_time_dict)
2.114551305770874
0.013440132141113281
So the answer is right and wrong, because using else
speeds it up considerably.
This suggests that the method of using an else: None
clause instead of a return None
is faster in general and it even makes the no dictionary method faster.
Interesting stuff indeed, can anyone explain to me why this is?
or
if you can not explain:
return your_thoughts