<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ashwinigajji</title>
    <description>The latest articles on Forem by ashwinigajji (@ashwinigajji).</description>
    <link>https://forem.com/ashwinigajji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F993626%2F8a4a205f-3514-41fb-affb-29e118b6fb8b.png</url>
      <title>Forem: ashwinigajji</title>
      <link>https://forem.com/ashwinigajji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ashwinigajji"/>
    <language>en</language>
    <item>
      <title>Ways to Speed Up Python Code</title>
      <dc:creator>ashwinigajji</dc:creator>
      <pubDate>Thu, 22 Dec 2022 12:12:02 +0000</pubDate>
      <link>https://forem.com/epam_india_python/ways-to-speed-up-python-code-3l7a</link>
      <guid>https://forem.com/epam_india_python/ways-to-speed-up-python-code-3l7a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Author&lt;/strong&gt;: Ashwini Gajji&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Read time&lt;/strong&gt;: 9 mins                                                                                        &lt;/p&gt;

&lt;p&gt;Python is easy to learn, has an excellent selection of open-source libraries, and has an extremely active and helpful community. &lt;/p&gt;

&lt;p&gt;When it comes to working with large quantities of data, Python can be really slow. Compared to working with languages like C and C++, Python can feel too slow at times. There are some fantastic libraries and built-in functions that can speed up Python code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prefer List Comprehension Over Loops
&lt;/h2&gt;

&lt;p&gt;List comprehension is an elegant and better way to create a new list based on the elements of an existing list in just a single line of code. &lt;/p&gt;

&lt;p&gt;List comprehension is considered a more Pythonic way to create a new list than defining an empty list and adding elements to that empty list. &lt;/p&gt;

&lt;p&gt;Another advantage of list comprehension is that it is faster than using the append method to add elements to a python list. &lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;Using list append method: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvouqep894d5qzey2tu4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvouqep894d5qzey2tu4b.png" alt="Image description" width="643" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A better way using list comprehension: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtqop887j1ss6hzss6ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtqop887j1ss6hzss6ag.png" alt="Image description" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code looks cleaner when using list comprehensions &lt;/p&gt;

&lt;h2&gt;
  
  
  Use Multiple Assignments
&lt;/h2&gt;

&lt;p&gt;To assign the values of multiple variables, no need to assign them line by line. Python has an elegant and better way to assign multiple variables. &lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjugo7uo8ynkqlszdcpr9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjugo7uo8ynkqlszdcpr9.png" alt="Image description" width="478" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A better way to assign these variables is: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwpu6llcbrl7ejrfti8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwpu6llcbrl7ejrfti8z.png" alt="Image description" width="729" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This assignment of variables is much cleaner and elegant than the above one. &lt;/p&gt;

&lt;h2&gt;
  
  
  Proper Import
&lt;/h2&gt;

&lt;p&gt;Avoid importing unnecessary modules and libraries until and unless we use them. Specify the module name instead of importing the complete library. Importing the unnecessary libraries will result in slowing down code performance. &lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;To find out the square root of a number. Instead of this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgcr12um06kfvmcj2y4yl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgcr12um06kfvmcj2y4yl.png" alt="Image description" width="800" height="49"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy2lfe29rk4z3isihuuww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy2lfe29rk4z3isihuuww.png" alt="Image description" width="800" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Calls Are Expensive
&lt;/h2&gt;

&lt;p&gt;Function calls are expensive in Python. While it is often good practice to separate code into functions, there are times should be cautious about calling functions from inside of a loop. It is better to iterate inside a function than to iterate and call a function each iteration. The following example where we want to create a list of cubed values in the range of 1–1,000,000. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faoxgzrrrk6gk8pbxdeyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faoxgzrrrk6gk8pbxdeyy.png" alt="Image description" width="554" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotx0lgtwl3o3sxcf51zg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotx0lgtwl3o3sxcf51zg.png" alt="Image description" width="672" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the second method was faster with all things equal other than using a function to cube the value. Functions have a lot of overhead, so for simple tasks like this they can add a lot of time relative to the total. &lt;br&gt;
              &lt;/p&gt;

&lt;h2&gt;
  
  
  Using Built-in Functions and Libraries
&lt;/h2&gt;

&lt;p&gt;Python’s built-in functions are one of the best ways to speed up code. Use built-in python functions whenever needed. These built-in functions are well tested and optimized. &lt;/p&gt;

&lt;p&gt;The reason these built-in functions are fast is that python’s built-in functions, such as min, max, all, map, etc., are implemented in the C language. &lt;/p&gt;

&lt;p&gt;Use these built-in functions instead of writing manual functions that will help you execute code faster. &lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanoqmygi996sbao3z491.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanoqmygi996sbao3z491.png" alt="Image description" width="632" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A better way to write this code is: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb09qcg9ev9ga6q1y454k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb09qcg9ev9ga6q1y454k.png" alt="Image description" width="800" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are using the built-in map function, which is written in C. Therefore, it is much faster than using a loop. &lt;/p&gt;

&lt;h2&gt;
  
  
  Proper Algorithm &amp;amp; Data Structure
&lt;/h2&gt;

&lt;p&gt;Each data structure has a significant effect on runtime. There are many built-in data structures such as list, tuple, set, and dictionary in python. Most people use a list data structure in all cases. &lt;/p&gt;

&lt;p&gt;In python, sets and dictionaries have O (1) lookup performance as they use hash tables for that. Use sets and dictionaries instead of lists in the following cases: &lt;/p&gt;

&lt;p&gt;You do not have duplicate items in the collection. &lt;/p&gt;

&lt;p&gt;You need to search items repeatedly in the collection. &lt;/p&gt;

&lt;p&gt;The collection contains many items. &lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Global Variables
&lt;/h2&gt;

&lt;p&gt;It is usually preferable to use local variables to keep better track of scope and memory usage. But beyond memory usage, Python is also slightly faster at retrieving local variables than global ones. So, it is simply best to avoid global variables when possible. &lt;/p&gt;

&lt;h2&gt;
  
  
  Vectorization in Python
&lt;/h2&gt;

&lt;p&gt;Vectorization is a technique of implementing array operations without using for loops. Instead, we use functions defined by various modules which are highly optimized that reduces the running and execution time of code. NumPy being a C implementation of arrays in Python provides vectorized actions on NumPy arrays. &lt;/p&gt;

&lt;p&gt;Method that adds elements using for loop and its Computation time: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcyau2rdni92a4nhjozg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcyau2rdni92a4nhjozg.png" alt="Image description" width="796" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Method that adds elements using vectorization and its Computation time: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bqle73wzzjwb0vnu9l0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bqle73wzzjwb0vnu9l0.png" alt="Image description" width="542" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see NumPy operations are faster than for loops. &lt;/p&gt;

&lt;p&gt;Max of array &lt;/p&gt;

&lt;p&gt;For finding the maximum element in an array, we can use for loop as well as python built-in methods max () . Let's compare ways with NumPy operations. &lt;/p&gt;

&lt;p&gt;Max () operation using for loop: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0t3in6evv6iq7p5rhbr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0t3in6evv6iq7p5rhbr.png" alt="Image description" width="766" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Max () operation using built-in method max (): &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6xiiyzgue1xxtpaig1vf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6xiiyzgue1xxtpaig1vf.png" alt="Image description" width="567" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Max () operation using numpy : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzh4m8e97b71kpigm4p7k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzh4m8e97b71kpigm4p7k.png" alt="Image description" width="673" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see NumPy operations are faster than built-in methods which are faster than for loops. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion :
&lt;/h2&gt;

&lt;p&gt;In this article, we have discussed some tricks that can be used to make python code run faster. These tips can be used especially with competitive programming where the time limit is everything. &lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=-Wks3CLHtsA" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=-Wks3CLHtsA&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://towardsdatascience.com/10-ways-to-speed-up-your-python-code-e3d57630b710" rel="noopener noreferrer"&gt;https://towardsdatascience.com/10-ways-to-speed-up-your-python-code-e3d57630b710&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.kdnuggets.com/2021/06/make-python-code-run-incredibly-fast.html" rel="noopener noreferrer"&gt;https://www.kdnuggets.com/2021/06/make-python-code-run-incredibly-fast.html&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/vectorization-in-python/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/vectorization-in-python/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.askpython.com/python-modules/numpy/vectorization-numpy" rel="noopener noreferrer"&gt;https://www.askpython.com/python-modules/numpy/vectorization-numpy&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimers:
&lt;/h2&gt;

&lt;p&gt;This is a personal blog. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally. &lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
