{"id":10662,"date":"2022-02-23T11:57:36","date_gmt":"2022-02-23T06:27:36","guid":{"rendered":"https:\/\/pynative.com\/?p=10662"},"modified":"2022-02-23T12:46:49","modified_gmt":"2022-02-23T07:16:49","slug":"python-get-execution-time-of-program","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-get-execution-time-of-program\/","title":{"rendered":"Python Measure the Execution Time of a Program"},"content":{"rendered":"\n
After reading this article, you’ll learn: –<\/p>\n\n\n\n
In this article, We will use the following four ways<\/strong> to measure the execution time in Python: –<\/p>\n\n\n\n To measure the code performance, we need to calculate the time taken by the script\/program to execute. Measuring the execution time of a program or parts of it will depend on your operating system, Python version, and what you mean by ‘time’.<\/p>\n\n\n\n Before proceeding further, first, understand what time is.<\/p>\n\n\n\n We often come across two terms to measure the execution time: Wall clock time and CPU time.<\/p>\n\n\n\n So it is essential to define and differentiate these two terms.<\/p>\n\n\n\n The difference between the Wall time and CPU time can occur from architecture and run-time dependency, e.g., programmed delays or waiting for system resources to become available.<\/p>\n\n\n\n For example, a program reports that it has used “CPU time 0m0.2s, Wall time 2m4s”. It means the program was active for 2 minutes and four seconds. Still, the computer’s processor spent only 0.2 seconds performing calculations for the program. May be program was waiting for some resources to become available.<\/p>\n\n\n\n At the beginning of each solution, I listed explicitly which kind of time each method measures<\/strong>.<\/p>\n\n\n\n So depending upon why you are measuring your program\u2019s execution time, you can choose to calculate the Wall or CPU time.<\/p>\n\n\n\ntime.time()<\/strong><\/code> function: measure the the total time elapsed to execute the script in seconds.<\/li>
time.process_time()<\/strong><\/code>: measure the CPU execution time of a code<\/li>
Table of contents<\/h2>
Wall time vs. CPU time<\/h2>\n\n\n\n
How to Measure <\/span>Execution Time in Python<\/h2>\n\n\n\n