{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/speed-up-python-code/","result":{"data":{"markdownRemark":{"id":"f623a310-1821-52ad-be68-612c260757de","excerpt":"A Few Ways to Speed Up Your Python Code Python is one of the most popular languages all over the world. Nowadays it is being used in competitive programming…","html":"<h1 id=\"a-few-ways-to-speed-up-your-python-code\" style=\"position:relative;\"><a href=\"#a-few-ways-to-speed-up-your-python-code\" aria-label=\"a few ways to speed up your python code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>A Few Ways to Speed Up Your Python Code</h1>\n<p>Python is one of the most popular languages all over the world. Nowadays it is being used in competitive programming also because of its simple syntax and rich libraries. Most of us probably started coding with python. At first, everything goes simple and easy. But while solving a hard algorithmic problem, most of us suffer from <code>Time Limit Exceeded</code>. However, it is not a problem of python; it is the programmer's problem. I am not saying that language is not slow, but if a programmer writes an efficient programme, it will get <code>Accepted</code> for sure. Here are some tips to speed up your python programme.</p>\n<h2 id=\"use-proper-data-structure\" style=\"position:relative;\"><a href=\"#use-proper-data-structure\" aria-label=\"use proper data structure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use proper data structure</h2>\n<p>Use of proper data structure has a significant effect on runtime. Python has list, tuple, set and dictionary as the built-in data structures. However, most of the people use the list in all cases. But it is not a right choice. Use proper data structures depending on your task. Especially use a tuple instead of a list. Because iterating over tuple is easier than iterating over a list.</p>\n<h2 id=\"decrease-the-use-of-for-loop\" style=\"position:relative;\"><a href=\"#decrease-the-use-of-for-loop\" aria-label=\"decrease the use of for loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decrease the use of for loop</h2>\n<p>As for loop is dynamic in python, it takes more time than while loop. So, use while loop instead of for loop.</p>\n<h2 id=\"use-list-comprehension\" style=\"position:relative;\"><a href=\"#use-list-comprehension\" aria-label=\"use list comprehension permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use list comprehension</h2>\n<p>Do not use any other technique if you can use list comprehension. For example, here is a code to list all the numbers between 1 and 1000 that is the multiplier of 3:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk11\">range</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1000</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> i%</span><span class=\"mtk7\">3</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">0</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        L.append (i)</span></span></code></pre>\n<p>Using list comprehension, it would be:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = [i </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk11\">range</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1000</span><span class=\"mtk1\">) </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> i%</span><span class=\"mtk7\">3</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></code></pre>\n<p>List comprehension works faster than using the append method.</p>\n<h2 id=\"use-multiple-assignments\" style=\"position:relative;\"><a href=\"#use-multiple-assignments\" aria-label=\"use multiple assignments permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use multiple assignments</h2>\n<p>Do not assaign variables like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">a = </span><span class=\"mtk7\">2</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">b = </span><span class=\"mtk7\">3</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">c = </span><span class=\"mtk7\">5</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">d = </span><span class=\"mtk7\">7</span></span></code></pre>\n<p>Instead, assign variables like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">a, b, c, d = </span><span class=\"mtk7\">2</span><span class=\"mtk1\">, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">, </span><span class=\"mtk7\">5</span><span class=\"mtk1\">, </span><span class=\"mtk7\">7</span></span></code></pre>\n<h2 id=\"do-not-use-global-variables\" style=\"position:relative;\"><a href=\"#do-not-use-global-variables\" aria-label=\"do not use global variables permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do not use global variables</h2>\n<p>Python has <code>global</code> keyword to declare global variables. But global variables take higher time during operation than a local variable. So, do not use global variables if it is not necessary.</p>\n<h2 id=\"use-library-function\" style=\"position:relative;\"><a href=\"#use-library-function\" aria-label=\"use library function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use library function</h2>\n<p>Do not write your function (manually) if it is already in the library. Library functions are highly efficient, and you will probably won't be able to code with that efficiency.</p>\n<h2 id=\"concatenate-strings-with-join\" style=\"position:relative;\"><a href=\"#concatenate-strings-with-join\" aria-label=\"concatenate strings with join permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concatenate strings with join</h2>\n<p>In python, you can concatenate strings with <code>+</code> operation.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">concatenatedString = </span><span class=\"mtk8\">&quot;Programming &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;is &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;fun.&quot;</span></span></code></pre>\n<p>It can also be done with <code>join()</code> method.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">concatenatedString = </span><span class=\"mtk8\">&quot; &quot;</span><span class=\"mtk1\">.join ([</span><span class=\"mtk8\">&quot;Programming&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;is&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;fun.&quot;</span><span class=\"mtk1\">])</span></span></code></pre>\n<p><code>join()</code> concatenates strings faster than <code>+</code> operation because <code>+</code> operators create a new string and then copies the old content at each step. But <code>join()</code> doesn't work that way.</p>\n<h2 id=\"use-generators\" style=\"position:relative;\"><a href=\"#use-generators\" aria-label=\"use generators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use generators</h2>\n<p>If you have a large amount of data in your list and you need to use one data at a time and for once then use <code>generator</code>s. It will save you time.</p>\n<h2 id=\"it-may-seem-efficient-but-its-not\" style=\"position:relative;\"><a href=\"#it-may-seem-efficient-but-its-not\" aria-label=\"it may seem efficient but its not permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>It may seem efficient, but it's not</h2>\n<p>See the below code:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> element </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk10\">set</span><span class=\"mtk1\">(L):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    ...</span></span></code></pre>\n<p>The above code may seem efficient because it used set to delete duplicate data. But the reality is that the code is not efficient. Do not forget that converting a list into set takes time. So this code will work better than the previous:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> element </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> L:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    ...</span></span></code></pre>\n<h2 id=\"do-not-use-dot-operation\" style=\"position:relative;\"><a href=\"#do-not-use-dot-operation\" aria-label=\"do not use dot operation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do not use dot operation</h2>\n<p>Try to avoid dot operation. See the below programme.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> math</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">val = math.sqrt(</span><span class=\"mtk7\">60</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Instead of the above style write code like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> math </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> sqrt</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">val = sqrt(</span><span class=\"mtk7\">60</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Because when you call a function using <code>.</code> (dot) it first calls <code>__getattribute()__</code> or <code>__getattr()__</code> which then use dictionary operation which costs time. So, try using <code>from module import function</code>.</p>\n<h2 id=\"use-1-for-infinity-loops\" style=\"position:relative;\"><a href=\"#use-1-for-infinity-loops\" aria-label=\"use 1 for infinity loops permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use 1 for infinity loops</h2>\n<p>Use <code>while 1</code> instead of <code>while True</code>. It will reduce some runtime.</p>\n<h2 id=\"try-a-different-approach\" style=\"position:relative;\"><a href=\"#try-a-different-approach\" aria-label=\"try a different approach permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Try a different approach</h2>\n<p>Try new ways to write your code efficiently. See the below code.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> a_condition:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> another_condition:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        do_something</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">else</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">raise</span><span class=\"mtk1\"> exception</span></span></code></pre>\n<p>Instead of the above code you can write:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">not</span><span class=\"mtk1\"> a_condition) </span><span class=\"mtk4\">or</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">not</span><span class=\"mtk1\"> another_condition):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">raise</span><span class=\"mtk1\"> exception</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">do_something</span></span></code></pre>\n<h2 id=\"use-speed-up-applications\" style=\"position:relative;\"><a href=\"#use-speed-up-applications\" aria-label=\"use speed up applications permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use speed up applications</h2>\n<p>For python's slow speed, some projects have been taken to decrease runtime. Pypy and Numba two of them. In most of the programming contests, you will see pypy if it allows python. These applications will reduce the runtime of your programme.</p>\n<h2 id=\"use-special-libraries-to-process-large-datasets\" style=\"position:relative;\"><a href=\"#use-special-libraries-to-process-large-datasets\" aria-label=\"use special libraries to process large datasets permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use special libraries to process large datasets</h2>\n<p>C/C++ is faster than python. So, many packages and modules have been written in C/C++ that you can use in your python programme. <code>Numpy</code>, <code>Scipy</code> and <code>Pandas</code> are three of them and are popular for processing large datasets.</p>\n<h2 id=\"use-the-latest-release-of-python\" style=\"position:relative;\"><a href=\"#use-the-latest-release-of-python\" aria-label=\"use the latest release of python permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use the latest release of python</h2>\n<p>Python is updated and upgraded regularly, and every release is faster and more optimized. So always use the latest version of python.</p>\n<p>These were some of the tips to decrease the runtime of python code. There are a few more techniques that you can use. Use a search engine to find those and write efficient code!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","headings":[{"value":"A Few Ways to Speed Up Your Python Code","depth":1},{"value":"Use proper data structure","depth":2},{"value":"Decrease the use of for loop","depth":2},{"value":"Use list comprehension","depth":2},{"value":"Use multiple assignments","depth":2},{"value":"Do not use global variables","depth":2},{"value":"Use library function","depth":2},{"value":"Concatenate strings with join","depth":2},{"value":"Use generators","depth":2},{"value":"It may seem efficient, but it's not","depth":2},{"value":"Do not use dot operation","depth":2},{"value":"Use 1 for infinity loops","depth":2},{"value":"Try a different approach","depth":2},{"value":"Use speed up applications","depth":2},{"value":"Use special libraries to process large datasets","depth":2},{"value":"Use the latest release of python","depth":2}],"fields":{"slug":"/engineering/speed-up-python-code/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"Learn a few ways to speed up your python code.","title":"Speed Up Python Code","canonical":null,"date":"October 15, 2020","updated_date":null,"tags":["Python","Performance"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/65558b9da57861b919205b297c81828b/2ad7f/speed-up-python-code-1.webp","srcSet":"/static/65558b9da57861b919205b297c81828b/1c9b5/speed-up-python-code-1.webp 200w,\n/static/65558b9da57861b919205b297c81828b/f1752/speed-up-python-code-1.webp 400w,\n/static/65558b9da57861b919205b297c81828b/2ad7f/speed-up-python-code-1.webp 800w,\n/static/65558b9da57861b919205b297c81828b/e7405/speed-up-python-code-1.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Md. Tahmid Hossain","github":"tahmid02016","bio":"I am a programmer, developer and digital-marketer.","avatar":null}}}},"pageContext":{"id":"f623a310-1821-52ad-be68-612c260757de","fields__slug":"/engineering/speed-up-python-code/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}