[Tut] Measure Execution Time with timeit() in Python - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Measure Execution Time with timeit() in Python (/thread-101271.html) |
[Tut] Measure Execution Time with timeit() in Python - xSicKxBot - 08-20-2023 [Tut] Measure Execution Time with timeit() in Python <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"1646475","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","starsonly":"","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","title":"Measure Execution Time with timeit() in Python","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </p></div> <h2 class="wp-block-heading">Understanding Timeit in Python</h2> <p>The <code>timeit</code> module is a tool in the Python standard library, designed to <strong>measure the execution time of small code snippets</strong>. It makes it simple for developers to analyze the performance of their code, allowing them to find areas for optimization.</p> <p class="has-global-color-8-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <code>timeit</code> module averages out various factors that affect the execution time, such as the <strong>system load</strong> and <strong>fluctuations in CPU performance</strong>. By running the code snippet <strong>multiple times and calculating an average execution time</strong>, it provides a more reliable measure of your code’s performance.</p> <p>To get started using <code>timeit</code>, simply import the module and use the <code>timeit()</code> method. This method accepts a code snippet as a <a href="https://blog.finxter.com/python-strings-made-easy/">string</a> and measures its execution time. Optionally, you can also pass the <code>number</code> parameter to specify how many times the code snippet should be executed. </p> <p>Here’s a quick example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_snippet = ''' def example_function(): return sum(range(10)) example_function() ''' execution_time = timeit.timeit(code_snippet, number=1000) print(f"Execution time: {execution_time:.6f} seconds") </pre> <p>Sometimes, you might want to evaluate a code snippet that requires additional imports or setup code. For this purpose, the <code>timeit()</code> method accepts a <code>setup</code> parameter where you can provide any necessary preparation code.</p> <p>For instance, if we adjust the previous example to include a required import:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="12" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_snippet = ''' def example_function(): return sum(range(10)) example_function() ''' setup_code = "import math" execution_time = timeit.timeit(code_snippet, setup=setup_code, number=1000) print(f"Execution time: {execution_time:.6f} seconds") </pre> <p>Keep in mind that <code>timeit</code> is primarily intended for small code snippets and may not be suitable for benchmarking large-scale applications.</p> <h2 class="wp-block-heading">Measuring Execution Time</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" fetchpriority="high" width="553" height="553" src="https://blog.finxter.com/wp-content/uploads/2023/08/Finxter_declutter_your_brain_gain_clarity_5a145cd2-0e2c-486b-9329-8b73addc668f.png" alt="" class="wp-image-1646465" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/Finxter_declutter_your_brain_gain_clarity_5a145cd2-0e2c-486b-9329-8b73addc668f.png 553w, https://blog.finxter.com/wp-content/uploads/2023/08/Finxter_declutter_your_brain_gain_clarity_5a145cd2-0e2c-486b-9329-8b73addc668f-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/Finxter_declutter_your_brain_gain_clarity_5a145cd2-0e2c-486b-9329-8b73addc668f-150x150.png 150w" sizes="(max-width: 553px) 100vw, 553px" /></figure> </div> <p>The primary method of measuring execution time with <code>timeit</code> is the <code>timeit()</code> function. This method runs the provided code repeatedly and returns the total time taken. <strong>By default, it repeats the code one million times</strong>! Be careful when measuring time-consuming code, as it may take a considerable duration.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_to_test = ''' example_function() ''' elapsed_time = timeit.timeit(code_to_test, number=1000) print(f'Elapsed time: {elapsed_time} seconds') </pre> <p>When using the <code>timeit()</code> method, the setup time is excluded from execution time. This way, the measurement is more accurate and focuses on the evaluated code’s performance, without including the time taken to configure the testing environment.</p> <p>Another useful method in the <code>timeit</code> module is <code>repeat()</code>, which calls the <code>timeit()</code> function multiple times and returns a <a href="https://blog.finxter.com/python-lists/">list</a> of results.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">results = timeit.repeat(code_to_test, repeat=3, number=1000) averaged_result = sum(results) / len(results) print(f'Average elapsed time: {averaged_result} seconds') </pre> <p>Sometimes it’s necessary to compare the execution speeds of different code snippets to identify the most efficient implementation. With the <code><a href="https://blog.finxter.com/pythons-time-clock-vs-time-time-detailed-comparsion/">time.time()</a></code> function, measuring the execution time of multiple code sections is simplified.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time start_time = time.time() first_example_function() end_time = time.time() elapsed_time_1 = end_time - start_time start_time = time.time() second_example_function() end_time = time.time() elapsed_time_2 = end_time - start_time print(f'First function elapsed time: {elapsed_time_1} seconds') print(f'Second function elapsed time: {elapsed_time_2} seconds') </pre> <p>In conclusion, using the <code>timeit</code> module and the <code>time.time()</code> function allows you to accurately measure and compare execution times in Python. </p> <h2 class="wp-block-heading">The Timeit Module</h2> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="877" height="1024" src="https://blog.finxter.com/wp-content/uploads/2023/08/pexels-photo-1461017-877x1024.jpeg" alt="" class="wp-image-1646478" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/pexels-photo-1461017-877x1024.jpeg 877w, https://blog.finxter.com/wp-content/uploads/2023/08/pexels-photo-1461017-257x300.jpeg 257w, https://blog.finxter.com/wp-content/uploads/2023/08/pexels-photo-1461017-768x897.jpeg 768w, https://blog.finxter.com/wp-content/uploads/2023/08/pexels-photo-1461017.jpeg 1284w" sizes="(max-width: 877px) 100vw, 877px" /></figure> </div> <p>To start using the <code>timeit</code> module, simply import it:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit </pre> <p>The core method in the <code>timeit</code> module is the <code>timeit()</code> method used to run a specific code snippet a given number of times, returning the total time taken. </p> <p>For example, suppose we want to measure the time it takes to <a href="https://blog.finxter.com/sum-of-square-roots-of-first-n-numbers-in-python/">square a list of numbers</a> using a <a href="https://blog.finxter.com/list-comprehension/">list comprehension</a>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_to_test = """ squared_numbers = [x**2 for x in range(10)] """ elapsed_time = timeit.timeit(code_to_test, number=1000) print("Time taken:", elapsed_time) </pre> <p>If you are using <strong>Jupyter Notebook</strong>, you can take advantage of the <code>%timeit</code> magic function to conveniently measure the execution time of a single line of code:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">%timeit squared_numbers = [x**2 for x in range(10)] </pre> <p>In addition to the <code>timeit()</code> method, the <code>timeit</code> module provides <code>repeat()</code> and <code>autorange()</code> methods. </p> <ul> <li>The <code>repeat()</code> method allows you to run the <code>timeit()</code> method multiple times and returns a list of execution times, while </li> <li>the <code>autorange()</code> method automatically determines the number of loops needed for a stable measurement.</li> </ul> <p>Here’s an example using the <code>repeat()</code> method:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_to_test = """ squared_numbers = [x**2 for x in range(10)] """ elapsed_times = timeit.repeat(code_to_test, number=1000, repeat=5) print("Time taken for each run:", elapsed_times) </pre> </p> <h2 class="wp-block-heading">Using Timeit Function</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="956" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-70.png" alt="" class="wp-image-1646479" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-70.png 956w, https://blog.finxter.com/wp-content/uploads/2023/08/image-70-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-70-768x512.png 768w" sizes="(max-width: 956px) 100vw, 956px" /></figure> </div> <p>To measure the execution time of a function, you can use the <code>timeit.timeit()</code> method. This method accepts two main arguments: the <code>stmt</code> and <code>setup</code>. The <code>stmt</code> is a string representing the code snippet that you want to time, while the <code>setup</code> is an optional string that can contain any necessary imports and setup steps. Both default to <code>'pass'</code> if not provided.</p> <p>Let’s say you have a function called <code>square()</code> that calculates the square of a given number:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def square(x): return x ** 2 </pre> <p>To measure the execution time of <code>square()</code> using <code>timeit</code>, you can do the following:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">results = timeit.timeit('square(10)', 'from __main__ import square', number=1000) </pre> <p>Here, we’re asking <code>timeit</code> to execute the <code>square(10)</code> function 1000 times and return the total execution time in seconds. You can adjust the <code>number</code> parameter to run the function for a different number of iterations.</p> <p>Another way to use <code>timeit</code>, especially for testing a callable function, is to use the <code>timeit.Timer</code> class. You can pass the <a href="https://blog.finxter.com/python-callable-function/">callable function</a> directly as the <code>stmt</code> parameter without the need for a setup string:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">timer = timeit.Timer(square, args=(10,)) results = timer.timeit(number=1000) </pre> <p>Now you have your execution time in the <code>results</code> variable, which you can analyze and compare with other functions’ performance.</p> </p> <h2 class="wp-block-heading">Examples and Snippets</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="469" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-71.png" alt="" class="wp-image-1646480" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-71.png 469w, https://blog.finxter.com/wp-content/uploads/2023/08/image-71-221x300.png 221w" sizes="(max-width: 469px) 100vw, 469px" /></figure> </div> <p>The simplest way to use <code>timeit.timeit()</code> is by providing a statement as a string, which is the code snippet we want to measure the execution time for. </p> <p>Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_snippet = "sum(range(100))" elapsed_time = timeit.timeit(code_snippet, number=1000) print(f"Execution time: {elapsed_time:.6f} seconds") </pre> <p>In the example above, we measure the time it takes to execute <code>sum(range(100))</code> 1000 times. The <code>number</code> parameter controls how many repetitions of the code snippet are performed. By default, <code>number=1000000</code>, but you can set it to any value you find suitable.</p> <p>For more complex code snippets with multiple lines, we can use <a href="https://blog.finxter.com/write-a-long-string-on-multiple-lines-in-python/">triple quotes to define a multiline string</a>:</p> <h2 class="wp-block-heading">Python Timeit Functions</h2> <p>The <code>timeit</code> module in Python allows you to accurately measure the execution time of small code snippets. It provides two essential functions: <code>timeit.timeit()</code> and <code>timeit.repeat()</code>.</p> <p class="has-base-2-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <code>timeit.timeit()</code> function measures the execution time of a given statement. You can pass the <code>stmt</code> argument as a string containing the code snippet you want to time. By default, <code>timeit.timeit()</code> will execute the statement 1,000,000 times and return the average time taken to run it. </p> <p>However, you can adjust the <code>number</code> parameter to specify a different number of iterations.</p> <p>For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_to_test = "sum(range(100))" execution_time = timeit.timeit(code_to_test, number=10000) print(execution_time)</pre> <p class="has-base-2-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <code>timeit.repeat()</code> function is a convenient way to call <code>timeit.timeit()</code> multiple times. It returns a list of timings for each repetition, allowing you to analyze the results more thoroughly. You can use the <code>repeat</code> parameter to specify the number of repetitions. </p> <p>Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit code_to_test = "sum(range(100))" execution_times = timeit.repeat(code_to_test, number=10000, repeat=5) print(execution_times) </pre> <p>In some cases, you might need to include additional setup code to prepare your test environment. You can do this using the <code>setup</code> parameter, which allows you to define the necessary setup code as a string. The execution time of the setup code will not be included in the overall timed execution.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit my_code = ''' def example_function(): return sum(range(100)) example_function() ''' setup_code = "from __main__ import example_function" result = timeit.timeit(my_code, setup=setup_code, number=1000) print(result) </pre> </p> <h2 class="wp-block-heading">Measuring Execution Time of Code Blocks</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="849" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-72.png" alt="" class="wp-image-1646481" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-72.png 849w, https://blog.finxter.com/wp-content/uploads/2023/08/image-72-300x225.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-72-768x576.png 768w" sizes="(max-width: 849px) 100vw, 849px" /></figure> </div> <p>The <code>timeit</code> module provides a straightforward interface for measuring the execution time of small code snippets. You can use this module to measure the time taken by a particular code block in your program. </p> <p>Here’s a brief example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit def some_function(): # Your code block here time_taken = timeit.timeit(some_function, number=1) print(f"Time taken: {time_taken} seconds") </pre> <p>In this example, the <code>timeit.timeit()</code> function measures the time taken to execute the <code>some_function</code> function. The <code>number</code> parameter specifies the number of times the function will be executed, which is set to 1 in this case.</p> <p>For more accurate results, you can use the <code>timeit.repeat()</code> function, which measures the time taken by the code block execution for multiple iterations. </p> <p>Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit def some_function(): # Your code block here repeat_count = 5 time_taken = timeit.repeat(some_function, number=1, repeat=repeat_count) average_time = sum(time_taken) / repeat_count print(f"Average time taken: {average_time} seconds") </pre> <p>In this example, the <code>some_function</code> function is executed five times, and the average execution time is calculated.</p> <p>Besides measuring time for standalone functions, you can also measure the time taken by individual code blocks inside a function. Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit def some_function(): # Some code here start_time = timeit.default_timer() # Code block to be measured end_time = timeit.default_timer() print(f"Time taken for code block: {end_time - start_time} seconds") </pre> <p>In this example, the <code>timeit.default_timer()</code> function captures the start and end times of the specified code block.</p> <h2 class="wp-block-heading">Using Timeit with Jupyter Notebook</h2> <div class="wp-block-image"> <figure class="aligncenter size-large is-resized"><img decoding="async" loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-73-1024x222.png" alt="Using Timeit with Jupyter Notebook" class="wp-image-1646482" style="width:733px;height:159px" width="733" height="159" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-73-1024x222.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/08/image-73-300x65.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-73-768x167.png 768w, https://blog.finxter.com/wp-content/uploads/2023/08/image-73.png 1042w" sizes="(max-width: 733px) 100vw, 733px" /></figure> </div> <p>Jupyter Notebook provides an excellent environment for running and testing Python code. <strong>To measure the execution time of your code snippets in Jupyter Notebook, you can use the <code>%timeit</code> and <code>%%timeit</code> magic commands, which are built into the IPython kernel.</strong></p> <p class="has-base-2-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <code>%timeit</code> command is used to measure the execution time of a single line of code. When using it, simply prefix your line of code with <code>%timeit</code>.</p> <p>For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">%timeit sum(range(100)) </pre> <p>This command will run the code multiple times and provide you with detailed statistics like the average time and standard deviation.</p> <p class="has-base-2-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> To measure the execution time of a code block spanning multiple lines, you can use the <code>%%timeit</code> magic command. Place this command at the beginning of a cell in Jupyter Notebook, and it will measure the execution time for the entire cell. </p> <p>For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">%%timeit total = 0 for i in range(100): total += i </pre> </p> <h2 class="wp-block-heading">Managing Garbage Collection and Overhead</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="954" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-74.png" alt="" class="wp-image-1646483" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-74.png 954w, https://blog.finxter.com/wp-content/uploads/2023/08/image-74-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-74-768x513.png 768w" sizes="(max-width: 954px) 100vw, 954px" /></figure> </div> <p>When using <code>timeit</code> in Python to measure code execution time, it is essential to be aware of the impact of garbage collection and overhead. </p> <p class="has-base-2-background-color has-background"><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f6af.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong><a href="https://blog.finxter.com/how-can-i-explicitly-free-memory-in-python/">Garbage collection</a></strong> is the process of automatically freeing up memory occupied by objects that are no longer in use. This can potentially impact the accuracy of <code>timeit</code> measurements if left unmanaged.</p> <p>By default, <code>timeit</code> disables garbage collection to avoid interference with the elapsed time calculations. However, you may want to include garbage collection in your measurements if it is a significant part of your code’s execution, or if you want to minimize the overhead and get more realistic results.</p> <p>To include garbage collection in <code>timeit</code> executions, you can use the <code>gc.enable()</code> function from the <code>gc</code> module and customize your <code>timeit</code> setup. </p> <p>Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit import gc mysetup = "import gc; gc.enable()" mycode = """ def my_function(): # Your code here pass my_function() """ elapsed_time = timeit.timeit(setup=mysetup, stmt=mycode, number=1000) print(elapsed_time) </pre> <p><strong>Keep in mind that including garbage collection will likely increase the measured execution time.</strong> Manage this overhead by balancing the need for accurate measurements with the need to see the impact of garbage collection on your code.</p> <p>Additionally, you can use the <code>timeit.repeat()</code> and <code>timeit.autorange()</code> methods to <a href="https://docs.python.org/3/library/timeit.html">measure execution time</a> of your code snippets multiple times, which can help you capture the variability introduced by garbage collection and other factors.</p> <h2 class="wp-block-heading">Choosing the Best Timer for Performance Measurements</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="956" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-75.png" alt="" class="wp-image-1646484" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-75.png 956w, https://blog.finxter.com/wp-content/uploads/2023/08/image-75-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-75-768x512.png 768w" sizes="(max-width: 956px) 100vw, 956px" /></figure> </div> <p>Measuring the execution time of your Python code is essential for optimization, and the <code>timeit</code> module offers multiple ways to achieve this. This section will focus on selecting the best timer for measuring performance.</p> <p>When using the <code>timeit</code> module, it is crucial to choose the right timer function. Different functions may provide various levels of accuracy and be suitable for different use cases. The two main timer functions are <code>time.process_time()</code> and <code>time.perf_counter()</code>.</p> <p><code>time.process_time()</code> measures the total CPU time used by your code, excluding any time spent during the sleep or wait state. This is useful for focusing on the computational efficiency of your code. This function is platform-independent and has a higher resolution on some operating systems. </p> <p>Here is an example code snippet:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time import timeit start = time.process_time() # Your code here end = time.process_time() elapsed = end - start print(f"Execution time: {elapsed} seconds") </pre> <p class="has-base-2-background-color has-background">On the other hand, <code>time.perf_counter()</code> measures the <a href="https://blog.finxter.com/how-to-measure-elapsed-time-in-python/">total elapsed time</a>, including sleep or wait states. This function provides a more accurate measurement of the total time required by your code to execute. This can help in understanding the real-world performance of your code. </p> <p>Here’s an example using <code>time.perf_counter()</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time import timeit start = time.perf_counter() # Your code here end = time.perf_counter() elapsed = end - start print(f"Execution time: {elapsed} seconds") </pre> <p>In addition to measuring execution time directly, you can also calculate the time difference using the <code><a href="https://blog.finxter.com/converting-string-to-datetime/">datetime</a></code> module. This module provides a more human-readable representation of time data. </p> <p>Here’s an example code snippet that calculates the time difference using <code>datetime</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from datetime import datetime start = datetime.now() # Your code here end = datetime.now() elapsed = end - start print(f"Execution time: {elapsed}") </pre> </p> <h2 class="wp-block-heading">Frequently Asked Questions</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="956" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/08/image-76.png" alt="" class="wp-image-1646485" srcset="https://blog.finxter.com/wp-content/uploads/2023/08/image-76.png 956w, https://blog.finxter.com/wp-content/uploads/2023/08/image-76-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/08/image-76-768x512.png 768w" sizes="(max-width: 956px) 100vw, 956px" /></figure> </div> <h3 class="wp-block-heading">How to measure function execution time using timeit?</h3> <p>To measure the execution time of a function using the <code>timeit</code> module, you can use the <code>timeit.timeit()</code> method. First, import the <code>timeit</code> module, and then create a function you want to measure. You can call the <code>timeit.timeit()</code> method with the function’s code and the number of executions as arguments.</p> <p>For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit def my_function(): # Your code here execution_time = timeit.timeit(my_function, number=1000) print("Execution time:", execution_time) </pre> <h3 class="wp-block-heading">What is the proper way to use the timeit module in Python?</h3> <p>The proper way to use the <code>timeit</code> module is by following these steps:</p> <ol> <li>Import the <code>timeit</code> module.</li> <li>Define the code or function to be timed.</li> <li>Use the <code>timeit.timeit()</code> method to measure the execution time, and optionally specify the number of times the code should be executed.</li> <li>Print or store the results for further analysis.</li> </ol> <h3 class="wp-block-heading">How to time Python functions with arguments using timeit?</h3> <p>To time a Python function that takes arguments using <code>timeit</code>, you can use a lambda function or <code>functools.partial()</code>. For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit from functools import partial def my_function(arg1, arg2): # Your code here # Using a lambda function time_with_lambda = timeit.timeit(lambda: my_function("arg1", "arg2"), number=1000) # Using functools.partial() my_function_partial = partial(my_function, "arg1", "arg2") time_with_partial = timeit.timeit(my_function_partial, number=1000) </pre> <h3 class="wp-block-heading">What are the differences between timeit and time modules?</h3> <p>The <code>timeit</code> module is specifically designed for measuring small code snippets’ execution time, while the <code>time</code> module is more generic for working with time-related functions. The <code>timeit</code> module provides more accurate and consistent results for timing code execution, as it disables the garbage collector and uses an internal loop, reducing the impact of external factors.</p> <h3 class="wp-block-heading">How to use timeit in a Jupyter Notebook?</h3> <p>In a Jupyter Notebook, use the <code>%%timeit</code> cell magic command to measure the execution time of a code cell:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">%%timeit # Your code here </pre> <p>This will run the code multiple times and provide the average execution time and standard deviation.</p> <h3 class="wp-block-heading">What is the best practice for measuring execution time with timeit.repeat()?</h3> <p>The <code>timeit.repeat()</code> method is useful when you want to measure the execution time multiple times and then analyze the results. The best practice is to specify the number of repeats, the number of loops per repeat, and analyze the results to find the fastest, slowest, or average time. For example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import timeit def my_function(): # Your code here repeat_results = timeit.repeat(my_function, number=1000, repeat=5) fastest_time = min(repeat_results) slowest_time = max(repeat_results) average_time = sum(repeat_results) / len(repeat_results) </pre> <p>Using <code>timeit.repeat()</code> allows you to better understand the function’s performance in different situations and analyze the variability in execution time.</p> <p><img decoding="async" src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-determine-script-execution-time-in-python/">How to Determine Script Execution Time in Python?</a></p> <p>The post <a rel="nofollow" href="https://blog.finxter.com/measure-execution-time-with-timeit-in-python/">Measure Execution Time with timeit() in Python</a> appeared first on <a rel="nofollow" href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p> </div> https://www.sickgaming.net/blog/2023/08/12/measure-execution-time-with-timeit-in-python/ |