{"id":110027,"date":"2022-11-07T17:58:03","date_gmt":"2022-11-07T17:58:03","guid":{"rendered":"https:\/\/ded9.com\/?p=110027"},"modified":"2025-11-23T10:40:11","modified_gmt":"2025-11-23T10:40:11","slug":"how-to-optimize-the-code-written-in-python","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/how-to-optimize-the-code-written-in-python\/","title":{"rendered":"How to Optimize Python Code for Better Performance"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Currently, Python is the most widely used programming language for building a Wide Variety Of Software Projects.\u00a0 <\/span>Statistics show that many programmers worldwide use Python to develop desktop and web-based applications. Of course, this doesn&#8217;t mean that Python developers are immune to sloppy and inefficient coding that can waste other developers&#8217; valuable time when reviewing code.<\/p>\n<p>Codes written in Python or other programming languages \u200b\u200bshould be carefully evaluated to eliminate code deficiencies and inefficiencies. Programmers must use a technique called &#8220;code optimization&#8221; to get projects noticed by clients and employers. Don&#8217;t forget the user-centered design pattern when building applications.<\/p>\n<p>Before we discuss the concept of code optimization and how to do it in\u00a0<strong>Python<\/strong>, we need to discuss user-centered design. User-centered design is one of the critical trends in software development, and it has attracted the attention of software teams and companies for several years.<\/p>\n<p>The potential benefits of design based on this tendency have been well demonstrated in various industries. Because most software developers have been working in this field for many years, and some work is based on the old design style, they may not initially favor user-centered design.<\/p>\n<p>User-centered design refers to a set of iterative processes in strategy, each stage focusing on assessing the user&#8217;s needs and requests to provide an efficient solution so that the users can use the developed product optimally. In user-centered design, design choices are heavily influenced by the user&#8217;s expectations, goals, and desires.<\/p>\n<p>In the above scenario, users work from the beginning to the end of the product design.<\/p>\n<p>An important point to note in this context is that user-centered design principles go beyond a simple user design, and designers are forced to develop products to meet the needs of a wide range of users. Typically, the above approach involves user research, interviews, usability testing, and feedback collection.<\/p>\n<p>In most cases, user-centered design is based on the following four steps:<\/p>\n<ul>\n<li>\u00a0Understanding the concept that the user has in mind<\/li>\n<li>\u00a0Determining needs and validating them<\/li>\n<li>\u00a0Providing efficient design solutions<\/li>\n<li>\u00a0Evaluation and repetition of development stages<\/li>\n<\/ul>\n<p>The ultimate goal of the user-centered design paradigm is to develop a product that is suitable for the user and what they need so that interaction with the product is trouble-free.<\/p>\n<p>User-centered design helps designers identify users&#8217; issues and allows them to use solutions that arise from the users&#8217; point of view in the development process.<\/p>\n<h2><span style=\"font-size: 18pt;\">What is optimization?<\/span><\/h2>\n<p>Let&#8217;s start by defining code optimization to get a basic idea and understand why we need <strong>it in Python<\/strong>. Sometimes, writing code that is supposed to do a sure thing is not enough.<br \/>\nLarge and inefficient codes can slow program execution, cause customers financial losses, or increase the time needed to evaluate and fix problems and bugs in the future.<\/p>\n<p>Optimizing <strong>Python<\/strong> code is a solution that allows you to write programs with fewer lines and less memory consumption. This provides the customer with a more efficient and smoother program that provides the desired results in the shortest possible time.<\/p>\n<p>It becomes essential when processing a large number of operations or data. Therefore, replacing and optimizing some blocks and features that may make running a program difficult is an important issue you should not easily pass by.<\/p>\n<p>Typically, code optimization is done for the following reasons:<\/p>\n<ul>\n<li>\u00a0Increase the speed and performance of the program<\/li>\n<li>\u00a0Increasing the readability of code<\/li>\n<li>\u00a0Easier debugging of errors<\/li>\n<li>\u00a0Saving the amount of CPU power and system resources<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\">6 top tips for optimizing Python code<\/span><\/h2>\n<p><strong>Python developers<\/strong><strong>\u00a0<\/strong>should use code optimization techniques instead of simple coding to ensure programs run faster. In the following, you will learn six practical tricks in code optimization that help programs run more smoothly and quickly.<\/p>\n<h3><span style=\"font-size: 14pt;\">1. Use Peephole Optimization Technique<\/span><\/h3>\n<p>Let&#8217;s start with how to run Python code to understand the Peephole optimization technique better. For this purpose, first, write the principles in a standard file. Next, run the following command<\/p>\n<p>python -m compiles all &lt;filename&gt;<\/p>\n<p>In the above command, you must specify the name of your file instead of the filename. After execution, you will receive the same file in *.pyc format, expressing the optimized code.<\/p>\n<p>The peephole is a code optimization technique in <strong>Python<\/strong> that improves compile time and code performance. Using the Peephole optimization technique, the codes are optimized behind the scenes, pre-calculations are performed on commands, and constant expressions or membership tests are performed on them.<\/p>\n<p>For example, you can write something like the number of seconds in a day as a = 60*60*24 to make the code more readable and allow the language interpreter to do the calculations quickly.<\/p>\n<p>The result of the Peephole optimization technique in the second example would be that <a href=\"https:\/\/ded9.com\/python-libraries-for-working-with-probability-distributions\/\"><strong>Python<\/strong> <\/a>would precompute the 24x60x60 constant expressions and replace them with 86400. Therefore, when it sees the above mathematical terms, it performs the calculations quickly, and the application&#8217;s performance is not reduced.<\/p>\n<h4><span style=\"font-size: 12pt;\">Why should we use the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Peephole\" target=\"_blank\" rel=\"noopener\">Peephole<\/a> optimization technique?<\/span><\/h4>\n<p>Using this method, you can replace a part of the program or part of the commands without making significant changes to the output. To be more precise, using this optimization method, you can do the following:<\/p>\n<p>Convert mutable structures to immutable. It can be done using one of the following three tuples:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">&lt;code__.co_varnames__&gt;: Saves local variables with specified parameters.\r\n&lt;code__.co_names__&gt;: holds global raw data.\r\n&lt;code__.co_consts__&gt;: Refers to all constants.<\/pre>\n<\/div>\n<p>You can confirm an element&#8217;s membership by treating commands as a fixed-cost operation regardless of the set size.<\/p>\n<h4>\u00a0Convert set and list to constants.<\/h4>\n<p>You should consider that the conversion process can only be done for raw data using <strong>Python<\/strong>. Hence, the optimization process will not be performed if the sets or lists you use have no raw data. To clarify the discussion, let&#8217;s look at some simple examples:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Def peephole_func():\r\n\r\nA=\" Hello, world\" * 5\r\n\r\nB= [1,2] *7\r\n\r\nC=(10,20,30) * 3\r\n\r\nPrint(a,b,c)<\/pre>\n<\/div>\n<p>The output of the above code snippet is shown in Figure 1.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-110031\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-1.jpeg\" alt=\"The output of the above code snippet is shown in Figure 1.\" width=\"575\" height=\"261\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-1.jpeg 575w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-1-300x136.jpeg 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/p>\n<h4 style=\"text-align: center;\">figure 1<\/h4>\n<p>The expression &#8220;Hello, world&#8221; * 5 is a fixed command of a length less than 4096. Therefore, it is deduced by the compiler as &#8220;Hello world&#8221; in 5 consecutive repetitions.<\/p>\n<p>The expression [1,2] *7 is a list (mutable object), so it is not evaluated.<\/p>\n<p>The expression (10,20,30) * 3 is a set of length 9, which is less than 256 (for tuples), so it is stored as (10, 20, 30, 10, 20, 30, 10, 20, 30).<\/p>\n<h3><span style=\"font-size: 14pt;\">2. Internalizing disciplines to achieve better performance<\/span><\/h3>\n<p>In programming languages like Python, string objects are sequences of Unicode characters; hence, in technical documentation, they are called text sequences. Python allocates extra information to store strings, which causes them to consume a lot of main memory space.<\/p>\n<p>When feelings of different sizes are added to a line, its overall size and weight increase, but this increase goes beyond adding a few characters.<\/p>\n<p>Python provides developers with a solution called string interning to improve performance.\u00a0String interning works by caching certain strings in memory during construction.\u00a0More precisely, only one instance of a particular thread is active at any given time, and no new memory is reserved to refer to it.<\/p>\n<p>String interning is very similar to shared objects. When a thread is interned, it is known as a shared object because\u00a0<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">all programs running in the same\u00a0<strong>Python<\/strong> session share an instance of that object globally<\/span>.<\/p>\n<p>In Python forks like Cython (CPython), once an interactive Python session is initialized, shared objects are loaded into memory. This is why string interning allows Python to work efficiently with strings, so that the time required for processing is reduced and less main memory is occupied for storing strings.<\/p>\n<h3><span style=\"font-size: 14pt;\">Identifier strings<\/span><\/h3>\n<p><b>Python&#8217;s<\/b> architecture\u00a0is such that it prefers to store only strings that are likely to be reused.\u00a0To be more precise, he is very interested in identifier strings.\u00a0Among these disciplines, the following should be mentioned:<\/p>\n<ul>\n<li>\u00a0Attribute names<\/li>\n<li>\u00a0The names of the variables<\/li>\n<li>\u00a0The terms of the arguments<\/li>\n<li>\u00a0Terms of functions and classes<\/li>\n<li>\u00a0Dictionary keys<\/li>\n<\/ul>\n<h4>The principles on which an intern course is based are as follows:<\/h4>\n<p>Only a string loaded as a static string at compile time will be internalized, and conversely, a series created at runtime will not be internalized. If a line results from a constant folding calculation, the continuous expressions are calculated at compile time instead of runtime.<br \/>\nStill, if they are longer than 20 characters, internalization is not performed on them because it is recognized whether they are identifiers No., it is hardly possible.<\/p>\n<p><strong>Python i<\/strong>nternalizes a string and creates a hash only if it is defined with a name that combines letters, numbers, and a letter or an underscore character.<br \/>\nHence, not all lines read from a file or received over the network are inalienable. Of course, a way to solve this problem is to load such lines with the intern() function.<\/p>\n<h3><span style=\"font-size: 14pt;\">3. Profiling codes<\/span><\/h3>\n<p>Code indexing is another way to optimize code. For this purpose, two options are available to developers.<\/p>\n<h4>Use &lt;timeit&gt;<\/h4>\n<p>This module calculates the time required to execute a task by a specific code block in milliseconds. Figure 2 shows how to call and execute the above module. The output of the module above is also shown in Figure 3.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-110034\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-2.jpeg\" alt=\"Figure 2 shows how to call and execute the above module.\" width=\"624\" height=\"223\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-2.jpeg 624w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-2-300x107.jpeg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<p style=\"text-align: center;\">figure 2<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-110037\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-3.jpeg\" alt=\"The output of the module above is also shown in Figure 3.\" width=\"608\" height=\"198\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-3.jpeg 608w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-3-300x98.jpeg 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/p>\n<p style=\"text-align: center;\">Figure 3<\/p>\n<h4>Using &lt;cProfile&gt;<\/h4>\n<p>cProfile is an advanced module and part of a software package first made available to developers with <strong>Python<\/strong>\u00a02.5.\u00a0Developers can add this module to\u00a0<strong>Python<\/strong>\u00a0code in the following ways :<\/p>\n<ul>\n<li>\u00a0Add a function to the run method, and then calculate the performance<\/li>\n<li>\u00a0Run the script from the command line while enabling cProfile as an argument using <b>Python&#8217;s<\/b>\u00a0m-option.<\/li>\n<\/ul>\n<p>For example, you can use the following syntax:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Python -m cProfile code.py arg1 arg2 arg3<\/pre>\n<\/div>\n<p>Knowing the key elements of the profile report allows you to find bottlenecks in your code. The critical thing to note is that the above technique only works with <strong>Python<\/strong> files, so you can&#8217;t index a <strong>Python<\/strong>\u00a0CLI by calling it directly like so:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Python -m cProfile my_cli<\/pre>\n<\/div>\n<p>To profile a CLI, you must provide the input path to cProfile. The syntax would be something like the following:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Python -m cProfile venv\/bin\/my_cli<\/pre>\n<\/div>\n<p>By executing the above command, after the program is finished, you will have an output table (Figure 4).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-110040\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-4.jpeg\" alt=\"By executing the above command, after the program is finished, you will have an output table (Figure 4).\" width=\"624\" height=\"200\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-4.jpeg 624w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-4-300x96.jpeg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<p style=\"text-align: center;\">Figure 4<\/p>\n<p>The elements in Figure 4 are as follows:<\/p>\n<ul>\n<li>&lt;ncalls&gt; indicates the number of calls created.<\/li>\n<li>&lt;tottime&gt; indicates the total time spent in the function.<\/li>\n<li>&lt;percall&gt; represents the coefficient of &lt;tottime&gt; divided by &lt;ncalls&gt;.<\/li>\n<li>&lt;cumtime&gt; is another important parameter that shows the cumulative execution time of the main and sub-functions.<\/li>\n<li>&lt;percall&gt; indicates the coefficient of &lt;cumtime&gt; divided by initial calls.<\/li>\n<li>&lt;filename_lineno(function)&gt; indicates the execution point of a program.<\/li>\n<\/ul>\n<h3><span style=\"font-size: 14pt;\">4. Using generators and keys for sorting<\/span><\/h3>\n<p>Generators and keys are robust solutions available to developers to optimize memory. One thing to note about generators is that they don&#8217;t return all items (iterators) at once, but can only return one item at a time.<br \/>\nWhen sorting elements in a list, it&#8217;s best to use the keys and the default &lt;sort()&gt; method. For example, you can sort lists and strings by the index selected for the critical argument. Figure 5 shows this.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-110043\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-5.jpeg\" alt=\"For example, you can sort lists and strings by the index selected for the critical argument. Figure 5 shows this.\" width=\"451\" height=\"403\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-5.jpeg 451w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/11\/word-image-110027-5-300x268.jpeg 300w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/p>\n<p style=\"text-align: center;\">Figure 5<\/p>\n<h3><span style=\"font-size: 14pt;\">5. Don&#8217;t neglect internal operators and external libraries<\/span><\/h3>\n<p>There are thousands of built-in operators and libraries available in\u00a0<strong>Python<\/strong>.\u00a0It is better to use Python&#8217;s built-in and predefined features as much as possible\u00a0to increase code performance.\u00a0Prebuilt operators and modules are pre-compiled, which speeds up the execution of applications.<\/p>\n<p>Some\u00a0<strong>Python<\/strong> libraries provide valuable features and capabilities that significantly increase the speed and performance of applications.\u00a0For example, use cPickle instead of Pickle to see the differences.\u00a0The PyPy and &lt;Cython&gt; packages provide a way to optimize a static compiler to make processes faster.<\/p>\n<h3><span style=\"font-size: 14pt;\">6. Do not use globals<\/span><\/h3>\n<p>Globals can have many obvious and hidden side effects that push your code into a style known as &#8220;spaghetti code.&#8221; Also, <strong>Python<\/strong> is slow to access external variables.\u00a0Therefore, it is better not to use them or limit their use.\u00a0If the use of globals is unavoidable and you have to use them, pay attention to the following two things:<\/p>\n<ul>\n<li>\u00a0Use the global keyword to define an external variable.<\/li>\n<li>\u00a0Make a local copy before using them inside loops.<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\">last word<\/span><\/h2>\n<p>Building a robust and scalable application that can perform assigned tasks quickly and optimally using main memory is essential. However, making such a program based on basic coding is impossible. That is why you need to optimize your <strong>Python code.<\/strong><\/p>\n<p>Suppose you pay attention to the optimization methods described in this article. In that case, you will not only write clean code but also improve the performance of your applications and shorten the code review process in the future.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I find which part of my Python code is slow?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use profiling tools like cProfile or line_profiler to identify bottlenecks.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Which built-in Python features help improve performance?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use built-in functions (map, filter), list comprehensions, and optimized libraries (NumPy) instead of manual loops.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can I reduce memory usage in performance-critical Python code?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use generators instead of lists, cache results with functools.lru_cache, and minimize unnecessary object creation.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Currently, Python is the most widely used programming language for building a Wide Variety Of Software Projects.\u00a0 Statistics show that many programmers worldwide use Python to develop desktop and web-based applications. Of course, this doesn&#8217;t mean that Python developers are immune to sloppy and inefficient coding that can waste other developers&#8217; valuable time when reviewing [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":110028,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[316],"tags":[320],"class_list":["post-110027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/110027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=110027"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/110027\/revisions"}],"predecessor-version":[{"id":265658,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/110027\/revisions\/265658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/110028"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=110027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=110027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=110027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}