|
1
|
This recipe allows one to use the "with" statement to time sections of code.
If one wishes to time specific regions of code, typically the following idiom is used: <pre>accumulated_time = 0 start = time.clock() Code to be timedend = time.clock() accumulated_time += end - start</pre> This gets tedious if many such blocks of code must be timed throughout a program. An alternative is to use the with statement, introduced in Python 2.5. This shortens the "code overhead" needed to time each section of code from three lines to one line.
Tags: algorithms
|
1 comment
Add a comment
Sign in to comment
Download
Copy to clipboard

An improvement. You could instead replace the final line of the function with
You would then use the function as follows:
This accumulates a list of times for each separate task, which you can then pass to the sum function for a final total: