Measuring Execution Time in the multiprocessing Testbed
In this lesson, you’ll extend your testbed to add some logging so you can trace how long it takes to calculate your result. You’ll measure the execution time with the
time.time() function, which we’ll use to compare the single-threaded and multithreaded implementations of the same algorithm.
Note: The code example here uses the
time.time() function to measure execution time. This is quite a simplistic approach (or potentially even incorrect, since
time.time() isn’t guaranteed to be monotonic). To learn more about how to accurately measure the execution time of your Python code, check out our Python Timer Functions tutorial.
In the next lesson, you’ll take a look at the
multiprocessing.Pool class and its parallel
map implementation, which make it a lot easier to parallelize most Python code that is written in a functional style.
and then I’m also going to measure the end time. What
time.time() does—it just gives you a seconds-based timestamp as a float, right? So, what we’re going to do here is we’re going to print the time to completion
and we’re just going to calculate that as
end - start. That’s going to give me the float in seconds that it took to run this piece of code here. Now when I save this, we can run this again. It’s going to very slowly process all of these records, one by one.
We’re simulating here that this would take up to
1 second and then it’s printing out, “Okay, this took seven seconds, and a little bit more,” which makes sense because we have seven records in here.
we get the processing as it happens—this is logging some stuff—and it tells us, “Hey, it took this long to calculate the
result and here it is in seconds.” We can make this a little bit more nice, just limit it to two decimals and have a really nice output.
Become a Member to join the conversation.