Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

How do I properly check the speed of my functions?

KilianS (3590 points)
7 10 27
in HW2 by (3.6k points)
I tried to take the time module and then take different ways to write a function and print out the time after in order to check the speed of the two functions. , but it always only gives me veeeery small differences.

Is there a better way to time it? or does that just mean that the functions actually all do run at kind of the same time
393 views
closed

3 Answers

Best answer
gianluca5539 (9820 points)
4 6 44
by (9.8k points)
selected by

Using a line profiler can be very helpful, just add @Profile on the line above your function declaration and run the profiler with kernprof, it will check line by line the time it took for that line to execute and also give you a percentage of the total function execution time. You obviously have to call that function in your "if name == main" so that it will execute. I recommend using very big datasets taken from the tests so you have better accuracy.

Check this line profiler, you will find all the information you need on this page.

Have a nice day!

KilianS (3590 points)
7 10 27
by (3.6k points)
nice day to you too, thanks!
andrea.sterbini (207920 points)
749 1267 2373
by (208k points)
And if you have Spyder 4 you can run the line profiler from within with the spyder-line-profiler plugin
Luigi Pizza (6120 points)
14 20 65
by (6.1k points)
edited by

import time

t0 = time.time()
code_block
t1 = time.time()

total = t1-t0

This does not get the average of several trials

Or you can use TimeIt

For more info click here

KilianS (3590 points)
7 10 27
by (3.6k points)
correct me if im wrong but i thought timeit only times like short code snippets? I wanna time the pace of the entire function that compares two strings, or the one that takes out the spaces...
Luigi Pizza (6120 points)
14 20 65
by (6.1k points)
Yes, you're right
KilianS (3590 points)
7 10 27
by (3.6k points)
ok thanks for the first idea tho
GabrielAlexandru (7760 points)
2 4 29
by (7.8k points)

If you installed the pytest-profiling library, then you can add --profile to the pytest command to see the time taken by the slowest functions. 

You can also use the spyder-line-profiler plugging for Spyder 4.

KilianS (3590 points)
7 10 27
by (3.6k points)
thanks i will try that