{"id":628,"date":"2018-04-07T04:46:44","date_gmt":"2018-04-07T04:46:44","guid":{"rendered":"http:\/\/www.afternerd.com\/blog\/?p=628"},"modified":"2019-01-13T03:29:02","modified_gmt":"2019-01-13T03:29:02","slug":"timeit-multiple-lines","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/timeit-multiple-lines\/","title":{"rendered":"Python: How to use the &#8216;timeit&#8217; module for multiple lines?"},"content":{"rendered":"<p>It is very useful to time small snippets of python code.<\/p>\n<p>For example, you might want to do that\u00a0to debug performance bottlenecks or to compare the efficiencies of different ways to perform a task.<\/p>\n<p>The <span class=\"symbol\">timeit<\/span>\u00a0module is a handy python module that allows you to do just that.<\/p>\n<p>Even though it is pretty straight forward to use this module to time one-line statements, you might find it a little tricky to time multiple-line statements.<\/p>\n<p>In this article, I will teach you how to use this awesome python module to time multiple lines.<\/p>\n<p>But before we dive into how to do that, let&#8217;s revisit the different ways you can use the <span class=\"symbol\">timeit<\/span> module.<\/p>\n<p>for example, say you want to time this statement<\/p>\n<pre class=\"prettyprint\"><code>L = [2 ** n for n in range(10)]<\/code><\/pre>\n<p>There are two ways you can do this<\/p>\n<h3>First: From python code<\/h3>\n<p>In your python code, you can jump import the module and use the timeit method.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; import timeit\r\n&gt;&gt;&gt; timeit.timeit('L = [2 ** n for n in range(10)]')\r\n3.0072080040117726<\/code><\/pre>\n<h3>Second: From the command line interface<\/h3>\n<p>Another way is to just invoke the timeit module from the shell like this<\/p>\n<pre class=\"prettyprint\"><code>$ python3 -m timeit 'L = [2 ** n for n in range(10)]'\r\n100000 loops, best of 3: 2.99 usec per loop<\/code><\/pre>\n<p>Seems pretty straightforward, right?<\/p>\n<p>Now how can you use the <span class=\"symbol\">timeit<\/span> module to time a snippet of python code that spans multiple lines?<\/p>\n<p>Let&#8217;s discuss how you can do that from python code and from the command line interface.<\/p>\n<h2>Timing multiple lines in python code<\/h2>\n<p>Timing multiple-line code snippets frome python code is very simple<\/p>\n<h3>Same indentation<\/h3>\n<p>The easiest way to time multiple lines having the same indentation is to use semicolons to separate the lines.<\/p>\n<p>For example, say you want to time the following snippet.<\/p>\n<pre class=\"prettyprint\"><code>x = 2\r\nL = [x ** n for n in range(10)]<\/code><\/pre>\n<p>your code will look like this<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; import timeit\r\n&gt;&gt;&gt; timeit.timeit('x = 2; L = [x ** n for n in range(10)]')\r\n3.0759821450337768<\/code><\/pre>\n<p>Easy peasy!<\/p>\n<p>But what if your code have different indentations?<\/p>\n<h3>Different indentations<\/h3>\n<p>The easiest way to handle different indentations is to to define your code snippet as a string first using triple quotes.<\/p>\n<p>The reason why you should use triple quotes is because it allows you to\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-format-text-in-python-3\" target=\"_blank\" rel=\"noopener\">define strings that can span multiple lines<\/a>.<\/p>\n<p>For example, you can do something like this<\/p>\n<pre class=\"prettyprint\"><code>import timeit\r\ns = \"\"\"\\\r\nL = []\r\nfor n in range(10):\r\n    L.append(2 ** n)\"\"\"\r\nt = timeit.timeit(s)<\/code><\/pre>\n<p>Prefect, now let&#8217;s discuss how you can do the same thing but from the command line interface.<\/p>\n<h2>Timing multiple lines in the command-line interface<\/h2>\n<h3>Same indentation<\/h3>\n<p>For multiple lines that have the same indentation, you can still use semicolons like you did in the previous case.<\/p>\n<pre class=\"prettyprint\"><code>$ python3 -m timeit 'x = 2; L = [x ** n for n in range(10)]'\r\n100000 loops, best of 3: 3.07 usec per loop<\/code><\/pre>\n<p>But this is not the only way.<\/p>\n<p>You can also specify each line as a separate statement argument like this<\/p>\n<pre class=\"prettyprint\"><code>$ python3 -m timeit 'x = 2' 'L = [x ** n for n in range(10)]'\r\n100000 loops, best of 3: 3.03 usec per loop<\/code><\/pre>\n<p>This separation of statements into different arguments allows us to specify indentations as well.<\/p>\n<p>Let&#8217;s see how.<\/p>\n<h3>Different indentations<\/h3>\n<p>For multiple lines with different indentations, you specify each line as a separate argument and you also preserve the leading spaces.<\/p>\n<p>For example, to time<\/p>\n<pre class=\"prettyprint\"><code>L = []\r\nfor n in range(10):\r\n    L.append(2 ** n)<\/code><\/pre>\n<p>you can do something like this:<\/p>\n<pre class=\"prettyprint\"><code>$ python3 -m timeit \\\r\n'L = []' 'for n in range(10):' '    L.append(2 ** n)'\r\n100000 loops, best of 3: 3.47 usec per loop<\/code><\/pre>\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[39,387,385,390,391,397,386,371,380,400,370,158,379,403,377,295,402,382,398,404],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":11,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1269,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions\/1269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/636"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=628"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}