{"id":19653,"date":"2022-02-07T09:33:38","date_gmt":"2022-02-07T04:03:38","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=19653"},"modified":"2022-02-07T09:33:41","modified_gmt":"2022-02-07T04:03:41","slug":"python-timeit","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-timeit\/","title":{"rendered":"Working With Python Timeit Library with Example"},"content":{"rendered":"\n<p>As we all know, python has a goldmine of handy libraries. In this article, let&#8217;s look at a beneficial method of measuring the execution time of your python code snippets. You guessed it right le. Let&#8217;s look at how we can use the Timeit() method. Timeit() is a built-in pythons module that provides a simple way to find the execution time of small bits of Python code. In this article, let&#8217;s take a look at you can use timeit().<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Installation_of_timeit\" >Installation of timeit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Python_timeit\" >Python timeit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Parameters_in_timeit\" >Parameters in timeit()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Basic_Example_of_timeit_Module\" >Basic Example of timeit Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Python_timeit_methods\" >Python timeit() methods<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Example_to_Understand_Two_Methods_of_Python_timeit_Module\" >Example to Understand Two Methods of Python timeit Module<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Python_timeit_Using_Command-line-Interface\" >Python timeit() Using Command-line-Interface<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Example_to_Understand_timeit_Using_Command-line-Interface\" >Example to Understand timeit() Using Command-line-Interface<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Python_timetime_Vs_timeittimeit\" >Python time.time Vs timeit.timeit<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Explanation\" >Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Is_timeit_in_Jupyter_Same_as_timeit\" >Is timeit in Jupyter Same as timeit()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Python_timeit_lambda\" >Python timeit lambda<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Reasons_why_timeit_is_better\" >Reasons why timeit() is better<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#FAQs_on_Python_timeit\" >FAQs on Python timeit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#Trending_Now\" >Trending Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-installation-of-timeit\"><span class=\"ez-toc-section\" id=\"Installation_of_timeit\"><\/span>Installation of timeit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Timeit() module comes built-in with python, so all you need to do is import it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# importing the required module\nimport timeit\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timeit\"><span class=\"ez-toc-section\" id=\"Python_timeit\"><\/span>Python timeit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Timeit() makes it accurate and straightforward to find out the execution time of small code snippets. We can use timeit() in both ways <strong>&nbsp;Command-line-interface(cli)<\/strong>&nbsp;or a <strong>callable<\/strong> one. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-parameters-in-timeit\"><span class=\"ez-toc-section\" id=\"Parameters_in_timeit\"><\/span>Parameters in timeit()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So , timeit( ) take four arguments which are <strong>timeit.timeit(stmt, setup, timer, number)<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>stmt<\/strong>  will take the code for which you want to measure the execution time. The default value is set to \u201cpass\u201d.<\/li><li><strong>setup<\/strong> will have setup details that need to be executed before stmt. The default value is set to \u201cpass.\u201d<\/li><li><strong>timer<\/strong> will have the <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/python-timer\/\" target=\"_blank\" rel=\"noreferrer noopener\">timer value<\/a><\/strong><\/span>, timeit() already has a default value set. So , we don&#8217;t have to worry about it.<\/li><li><strong>number<\/strong> will take the number of execution you want to run on <strong>smt<\/strong>. The default value is set to 1000000.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-basic-example-of-timeit-module\"><span class=\"ez-toc-section\" id=\"Basic_Example_of_timeit_Module\"><\/span>Basic Example of timeit Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Let&#8217;s take a look at an example for a better understanding.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timeit\n\n# code snippet to be executed only once\nsetup_details = &quot;import random&quot;\n# code snippet whose execution time is to be measured\nsamplecode = '''\ndef example():\n    somenumber = random.randit(0,50)\n    return somenumber\n'''\n# timeit statement\nprint (&quot;Execution time of program:-&quot;, timeit.timeit(setup = setup_details,\n                     stmt = samplecode,\n                     number = 10000))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nExecution time of program:- 0.0017167000000000016\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timeit-methods\"><span class=\"ez-toc-section\" id=\"Python_timeit_methods\"><\/span>Python timeit() methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two methods present in the timeit( ). The two methods are<strong> timeit.default_timer()<\/strong>&nbsp;and <strong>timeit.repeat(stmt, setup, timer, repeat, number)<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>timeit.default_timer()<\/strong> will  provide the best clock available on your platform.<\/li><li> <strong>timeit.repeat(stmt, setup, timer, repeat, number)<\/strong> is similar to the timeit( ) method we used preiously with added feature of <strong>repeat<\/strong> which will call the the timeit() the number of times repeat is mentioned.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s better understand these two methods using an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-to-understand-two-methods-of-python-timeit-module\"><span class=\"ez-toc-section\" id=\"Example_to_Understand_Two_Methods_of_Python_timeit_Module\"><\/span>Example to Understand Two Methods of Python timeit Module<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-example-1\">Example 1:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timeit                          #using timeit.default()\nstart = timeit.default_timer()\nfor i in range(0,50):\n     pass\nend = timeit.default_timer()\nprint(end - start)\n<\/pre><\/div>\n\n\n<p><strong>#Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n6.399999999996\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"example-2\">Example 2<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timeit                  #using timeit.repeat()\nsetup_details = &quot;import random&quot;\nsamplecode = '''\ndef example():\n    somenumber = random.randit(0,50)\n    return somenumber\n'''\nprint (&quot;Execution time of program:-&quot;, timeit.repeat(setup = setup_details,\n                     stmt = samplecode,repeat=5))\n<\/pre><\/div>\n\n\n<p><strong>#Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nExecution time of program:- &#x5B;0.1489412, 0.14861209999999997, 0.15124629999999994, 0.15548090000000003, 0.1436058]\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-juliett monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-image\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/a><\/div><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timeit-using-command-line-interface\"><span class=\"ez-toc-section\" id=\"Python_timeit_Using_Command-line-Interface\"><\/span>Python timeit() Using <strong>Command-line-Interface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>As mentioned above, we can also use timeit() in the cli.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython -m timeit &#x5B;-n N] &#x5B;-r N] &#x5B;-u U] &#x5B;-s S] &#x5B;-h] &#x5B;code ...]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>-m<\/strong> : this will take the positional argument in our case its <strong>timeit<\/strong><\/li><li><strong>-n N:<\/strong>  will take the number of execution you want to run on code .<\/li><li><strong>-r N:<\/strong> <span style=\"font-size: inherit\">This allow yo to mention the number of time <\/span>you want to repeat timeit( ) .<\/li><li><strong>-s S<\/strong>: This will allow you y=to setup details that need to be executed before code.<\/li><li><strong>-h:<\/strong> for help.<\/li><li><strong>code <\/strong>: The code details.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-to-understand-timeit-using-command-line-interface\"><span class=\"ez-toc-section\" id=\"Example_to_Understand_timeit_Using_Command-line-Interface\"><\/span>Example to Understand timeit() Using <strong>Command-line-Interface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nC:\\Pythonpool\\&gt;python -m timeit -s &quot;from math import sqrt&quot; -n 10000 -r 4 &quot;y = sqrt(12)&quot;\n<\/pre><\/div>\n\n\n<p><strong>#Output <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n10000 loops, best of 4: 81.4 nsec per loop\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-time-time-vs-timeit-timeit\"><span class=\"ez-toc-section\" id=\"Python_timetime_Vs_timeittimeit\"><\/span>Python time.time Vs timeit.timeit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, you might be asking why timeit( ) why can&#8217;t you use time module to calculate the time taken by code snippet by doing these for example:-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\nstart = time.time()\nfor i in range(0,6):\n    print(i)\ntime.sleep(1)\nend = time.time()\nprint(f&quot;Total runtime of the program is {end - start}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>#Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n0\n1\n2\n3\n4\n5\nTotal runtime of the program is 1.0139861106872559\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-explanation\"><span class=\"ez-toc-section\" id=\"Explanation\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Well, you see the problem with using the time module to calculate the execution time of small code snippets is that they are not that accurate because the results produced by time.time may also be affected by some other process that was running in the background in your system at that time.<\/p>\n\n\n\n<p>But the timeit module is great for calculating the execution time of small code snippets because timeit runs your snippet of code 1millions of times (default value is 1000000) &nbsp;and provides the minimum time taken from the given set of code snippets, and it is pretty easy to use.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-lima monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-image\"><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/a><\/div><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-is-timeit-in-jupyter-same-as-timeit\"><span class=\"ez-toc-section\" id=\"Is_timeit_in_Jupyter_Same_as_timeit\"><\/span>Is timeit in Jupyter Same as timeit()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Yes, the %timeit magic command in jupyter is similar to the timeit() we discussed here.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"842\" height=\"180\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Screenshot-12.png\" alt=\"Python Timeit() Libary Jupyter\" class=\"wp-image-19707\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Screenshot-12.png 842w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Screenshot-12-300x64.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Screenshot-12-768x164.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><figcaption>    <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timeit-lambda\"><span class=\"ez-toc-section\" id=\"Python_timeit_lambda\"><\/span>Python timeit lambda<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use timeit() with lambda functions, but it&#8217;s not recommended when testing small code snippets since it adds overhead.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timeit\nprint(timeit.timeit(lambda: 4 + 5))\nprint(timeit.timeit(&quot;output = 4 + 5&quot;))\n<\/pre><\/div>\n\n\n<p><strong>#Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n0.10917500000000001\n0.02280180000000001\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-reasons-why-timeit-is-better\"><span class=\"ez-toc-section\" id=\"Reasons_why_timeit_is_better\"><\/span>Reasons why timeit() is better<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>It <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/numpy-repeat\/\" target=\"_blank\" rel=\"noreferrer noopener\">repeats<\/a><\/strong><\/span> the tests many times to eliminate the influence of other background tasks on your machine for example <strong>os scheduling<\/strong>.<\/li><li>It also disables the <span style=\"text-decoration: underline;\"><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Garbage_collection_(computer_science)\" target=\"_blank\" rel=\"noreferrer noopener\">garbage<\/a><\/strong><\/span> collector for more precise result.<\/li><\/ul>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-beta monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-image\"><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/a><\/div><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs-on-python-timeit\"><span class=\"ez-toc-section\" id=\"FAQs_on_Python_timeit\"><\/span>FAQs on Python timeit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1643984173891\"><strong class=\"schema-faq-question\">How to convert python timeit to string?<br\/><\/strong> <p class=\"schema-faq-answer\">By using <strong>str( timeit(&#8230;.))<\/strong> we can convert the timeit() return value as string.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1643984723568\"><strong class=\"schema-faq-question\">How to pass multiple parameters to timeit.Timer?<br\/><\/strong> <p class=\"schema-faq-answer\">Currently, Timer() only supports 4 parameters, namely, stmt, setup, timer, and globals. You can pass all the parameters at one line by using Timer(stmt=&#8217;pass&#8217;, setup=&#8217;pass&#8217;, timer=, globals=None). Use arguments that correspond to each of these parameters and initialize the class. <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1644202213334\"><strong class=\"schema-faq-question\">What are the alternatives to <strong>Python timeit <\/strong>library?<\/strong> <p class=\"schema-faq-answer\">Alternatives to timeit() are the python <strong>time module,<\/strong> <strong>Timerit<\/strong> extension.<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In summary, timeit() is a handy module for finding out the execution time of small code snippets and can be very handy to know.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Now\"><\/span>Trending Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>As we all know, python has a goldmine of handy libraries. In this article, let&#8217;s look at a beneficial method of measuring the execution time &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Working With Python Timeit Library with Example\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/#more-19653\" aria-label=\"More on Working With Python Timeit Library with Example\">Read more<\/a><\/p>\n","protected":false},"author":27,"featured_media":19769,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4345],"tags":[4842,4838,4844,4841,4843,4840,4839],"class_list":["post-19653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","tag-how-to-use-timeit-python","tag-python-timeit-decorator","tag-python-timeit-example","tag-python-timeit-function","tag-timeit-function-python","tag-timeit-in-python","tag-timeit-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Working With Python Timeit Library with Example - Python Pool<\/title>\n<meta name=\"description\" content=\"Python timeit module is great for calculating the execution time of small code snippets because timeit runs your snippet of code 1millions of times (default value is 1000000).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-timeit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working With Python Timeit Library with Example\" \/>\n<meta property=\"og:description\" content=\"As we all know, python has a goldmine of handy libraries. In this article, let&#039;s look at a beneficial method of measuring the execution time of your\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-timeit\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-07T04:03:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-07T04:03:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Vishnu Nair\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishnu Nair\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/\"},\"author\":{\"name\":\"Vishnu Nair\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/99bc471551c7b47aa64d165b3fd6fa99\"},\"headline\":\"Working With Python Timeit Library with Example\",\"datePublished\":\"2022-02-07T04:03:38+00:00\",\"dateModified\":\"2022-02-07T04:03:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/\"},\"wordCount\":770,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp\",\"keywords\":[\"how to use timeit python\",\"python timeit decorator\",\"python timeit example\",\"python timeit function\",\"timeit function python\",\"timeit in python\",\"timeit python\"],\"articleSection\":[\"Module\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-timeit\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-timeit\/\",\"name\":\"Working With Python Timeit Library with Example - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp\",\"datePublished\":\"2022-02-07T04:03:38+00:00\",\"dateModified\":\"2022-02-07T04:03:41+00:00\",\"description\":\"Python timeit module is great for calculating the execution time of small code snippets because timeit runs your snippet of code 1millions of times (default value is 1000000).\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-timeit\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp\",\"width\":1200,\"height\":628,\"caption\":\"Python Timeit\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Working With Python Timeit Library with Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/99bc471551c7b47aa64d165b3fd6fa99\",\"name\":\"Vishnu Nair\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/27195e2368798ebad8841e38c0caeaba7058b78e5342c1857d1270a255bec7db?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/27195e2368798ebad8841e38c0caeaba7058b78e5342c1857d1270a255bec7db?s=96&d=wavatar&r=g\",\"caption\":\"Vishnu Nair\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891\",\"name\":\"How to convert python timeit to string?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"By using <strong>str( timeit(....))<\/strong> we can convert the timeit() return value as string.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568\",\"name\":\"How to pass multiple parameters to timeit.Timer?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Currently, Timer() only supports 4 parameters, namely, stmt, setup, timer, and globals. You can pass all the parameters at one line by using Timer(stmt='pass', setup='pass', timer=, globals=None). Use arguments that correspond to each of these parameters and initialize the class. \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334\",\"name\":\"What are the alternatives to Python timeit library?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Alternatives to timeit() are the python <strong>time module,<\/strong> <strong>Timerit<\/strong> extension.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Working With Python Timeit Library with Example - Python Pool","description":"Python timeit module is great for calculating the execution time of small code snippets because timeit runs your snippet of code 1millions of times (default value is 1000000).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-timeit\/","og_locale":"en_US","og_type":"article","og_title":"Working With Python Timeit Library with Example","og_description":"As we all know, python has a goldmine of handy libraries. In this article, let's look at a beneficial method of measuring the execution time of your","og_url":"https:\/\/www.pythonpool.com\/python-timeit\/","og_site_name":"Python Pool","article_published_time":"2022-02-07T04:03:38+00:00","article_modified_time":"2022-02-07T04:03:41+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp","type":"image\/webp"}],"author":"Vishnu Nair","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Vishnu Nair","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/"},"author":{"name":"Vishnu Nair","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/99bc471551c7b47aa64d165b3fd6fa99"},"headline":"Working With Python Timeit Library with Example","datePublished":"2022-02-07T04:03:38+00:00","dateModified":"2022-02-07T04:03:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/"},"wordCount":770,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp","keywords":["how to use timeit python","python timeit decorator","python timeit example","python timeit function","timeit function python","timeit in python","timeit python"],"articleSection":["Module"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-timeit\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-timeit\/","url":"https:\/\/www.pythonpool.com\/python-timeit\/","name":"Working With Python Timeit Library with Example - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp","datePublished":"2022-02-07T04:03:38+00:00","dateModified":"2022-02-07T04:03:41+00:00","description":"Python timeit module is great for calculating the execution time of small code snippets because timeit runs your snippet of code 1millions of times (default value is 1000000).","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891"},{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568"},{"@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-timeit\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/02\/Python-Timeit.webp","width":1200,"height":628,"caption":"Python Timeit"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Working With Python Timeit Library with Example"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/99bc471551c7b47aa64d165b3fd6fa99","name":"Vishnu Nair","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/27195e2368798ebad8841e38c0caeaba7058b78e5342c1857d1270a255bec7db?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/27195e2368798ebad8841e38c0caeaba7058b78e5342c1857d1270a255bec7db?s=96&d=wavatar&r=g","caption":"Vishnu Nair"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891","position":1,"url":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984173891","name":"How to convert python timeit to string?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"By using <strong>str( timeit(....))<\/strong> we can convert the timeit() return value as string.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568","position":2,"url":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1643984723568","name":"How to pass multiple parameters to timeit.Timer?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Currently, Timer() only supports 4 parameters, namely, stmt, setup, timer, and globals. You can pass all the parameters at one line by using Timer(stmt='pass', setup='pass', timer=, globals=None). Use arguments that correspond to each of these parameters and initialize the class. ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334","position":3,"url":"https:\/\/www.pythonpool.com\/python-timeit\/#faq-question-1644202213334","name":"What are the alternatives to Python timeit library?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Alternatives to timeit() are the python <strong>time module,<\/strong> <strong>Timerit<\/strong> extension.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/19653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=19653"}],"version-history":[{"count":49,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/19653\/revisions"}],"predecessor-version":[{"id":19777,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/19653\/revisions\/19777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/19769"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=19653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=19653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=19653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}