{"id":269094,"date":"2025-06-13T09:44:22","date_gmt":"2025-06-13T09:44:22","guid":{"rendered":"https:\/\/imarticus.org\/blog\/?p=269094"},"modified":"2025-06-30T09:44:42","modified_gmt":"2025-06-30T09:44:42","slug":"how-to-optimise-python-code-for-better-performance","status":"publish","type":"post","link":"https:\/\/imarticus.org\/blog\/how-to-optimise-python-code-for-better-performance\/","title":{"rendered":"How to Optimise Python Code for Better Performance"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">It is a truth known that a well-made app or website has its foundation in a well-built code. Developers have a tendency to obsess over performance. Most of your codebase probably runs just fine and doesn\u2019t impact the overall speed of your app. Especially for scripts that run on a schedule or behind the scenes (like ETL jobs), performance usually isn&#8217;t a big deal.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">However, performance becomes absolutely crucial when it&#8217;s tied to user experience. If your app takes too long to load or respond, people will notice, and obviously, no one likes waiting. The bottleneck is usually restricted to one or two parts of the codebase. Fixing that improves the overall performance. <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">In this blog, we will tackle strategies to optimise <\/span><span style=\"font-weight: 400;\">Python code<\/span><span style=\"font-weight: 400;\"> so you can address your code performance issues. Meanwhile, you can look into the <\/span><a href=\"https:\/\/imarticus.org\/postgraduate-program-in-data-science-analytics\/\"><span style=\"font-weight: 400;\">Data Science course<\/span><\/a><span style=\"font-weight: 400;\"> by Imarticus Learning to diversify your career and get practical training in Python, SQL, Tableau, Power BI, etc.\u00a0<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Why You Should Care About <\/span><span style=\"font-weight: 400;\">Python Code<\/span><span style=\"font-weight: 400;\"> Performance<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Let\u2019s be honest \u2014 most of us don\u2019t start worrying about performance until something breaks. But sloppy code can creep up on you.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Maybe you\u2019re working with large datasets<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Or you\u2019re automating reports that suddenly take 10 minutes instead of 30 seconds<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Or your backend just can\u2019t keep up with API requests<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">That\u2019s when <\/span><span style=\"font-weight: 400;\">Python code optimisation<\/span><span style=\"font-weight: 400;\"> becomes your fallback guy.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And don\u2019t worry, you don\u2019t need to be some 10x dev to make your code faster. Small changes can go a long way.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">1. Use Built-in Functions Wherever Possible<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Python has a massive standard library. And most of it is built in C under the hood, which means it\u2019s much faster than your hand-written loops.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Slower way<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">squared = []<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for i in range(1000):<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0squared.append(i*i)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Faster way<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">squared = list(map(lambda x: x*x, range(1000)))<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Even better<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">squared = [i*i for i in range(1000)]<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">That last one\u2019s not just faster, it\u2019s cleaner too.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Read: <\/span><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html\"><span style=\"font-weight: 400;\">Built-in Functions \u2014 Python 3.13.2 documentation<\/span><\/a><\/p>\n<h3><span style=\"font-weight: 400;\">2. Profile First, Optimise Later<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">You can\u2019t fix what you can\u2019t measure.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Start with the cProfile module. Just run:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\">python -m cProfile myscript.py<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">You\u2019ll get a full breakdown of which parts of your script are slowing things down. Focus your <\/span><span style=\"font-weight: 400;\">Python code optimisation<\/span><span style=\"font-weight: 400;\"> efforts there.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can also use tools like:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">line_profiler<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">memory_profiler<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Py-Spy (very handy)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Watch this<\/span><a href=\"https:\/\/www.youtube.com\/watch?v=3h1ZUFONFzA&amp;list=PLGnEY8uzzUsNZz0WHjmDXILGV0yAWgx_n&amp;index=9\"><span style=\"font-weight: 400;\"> ERROR HANDLING in Python \u2013 Write Robust &amp; Bug-Free Code Python <\/span><\/a><span style=\"font-weight: 400;\">by Imarticus learning<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">3. Avoid Using Global Variables<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">This one&#8217;s sneaky. Global variables slow things down because Python has to look them up in a different scope. It\u2019s a small hit, but over many iterations, it adds up.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Bad<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">counter = 0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">def increment():<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0global counter<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0counter += 1<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Better<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">def increment(counter):<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return counter + 1<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Keep variables local whenever possible.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">4. Use Generators Instead of Lists When You Can<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Generators are lazy. That\u2019s a good thing. They don\u2019t compute anything until you actually need it.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Compare:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Uses memory upfront<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">nums = [i for i in range(1000000)]<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Efficient<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">nums = (i for i in range(1000000))<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">If you\u2019re just looping through data once, use generators. It saves a ton of memory and can improve performance in tight loops.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">5. Don\u2019t Recalculate Stuff You Already Know<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Caching is your friend. Especially with expensive operations.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Use functools.lru_cache:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\">from functools import lru_cache<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">@lru_cache(maxsize=None)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">def fib(n):<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if n &lt; 2:<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return n<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return fib(n-1) + fib(n-2)<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">This will save previously calculated results and reuse them.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">6. Use NumPy for Heavy Math<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">If your <\/span><span style=\"font-weight: 400;\">Python code<\/span><span style=\"font-weight: 400;\"> does a lot of number crunching, NumPy is a game-changer.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Why? Because<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">It uses C in the background<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">It works with arrays faster than native Python lists<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">It&#8217;s super optimised<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>Task<\/b><\/td>\n<td><b>Native Python<\/b><\/td>\n<td><b>NumPy<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Summing a million numbers<\/span><\/td>\n<td><span style=\"font-weight: 400;\">~50ms<\/span><\/td>\n<td><span style=\"font-weight: 400;\">~5ms<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Matrix Multiplication<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sluggish<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Super fast<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Here\u2019s an example:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\">import numpy as np<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">a = np.arange(1000000)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">b = a * 2<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">That\u2019s it. Blazing fast.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Read: <\/span><a href=\"https:\/\/numpy.org\/doc\/2.2\/user\/absolute_beginners.html\"><span style=\"font-weight: 400;\">the absolute basics for beginners \u2014 NumPy v2.2 Manual<\/span><\/a><\/p>\n<h3><span style=\"font-weight: 400;\">7. Use Pandas with Care<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Pandas is great. But not always fast.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Some tips to <\/span><span style=\"font-weight: 400;\">optimise Python scripts<\/span><span style=\"font-weight: 400;\"> with Pandas:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use <\/span><i><span style=\"font-weight: 400;\">.loc[]<\/span><\/i><span style=\"font-weight: 400;\"> or <\/span><i><span style=\"font-weight: 400;\">.iloc[]<\/span><\/i><span style=\"font-weight: 400;\"> instead of chained indexing<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Avoid row-wise operations; go vectorised<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use <\/span><i><span style=\"font-weight: 400;\">categorical<\/span><\/i><span style=\"font-weight: 400;\"> dtype when dealing with repeating strings<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Drop unnecessary columns before heavy operations<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Check this <\/span><a href=\"https:\/\/towardsdatascience.com\/advanced-pandas-techniques-for-data-processing-and-performance-23f9026d21f5\/\"><span style=\"font-weight: 400;\">Advanced Pandas Techniques for Data Processing and Performance<\/span><\/a><\/p>\n<h3><span style=\"font-weight: 400;\">8. Avoid Repeated Function Calls in Loops<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Even a simple function can add overhead when called repeatedly in a loop.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Slower<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for i in range(len(my_list)):<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0process(my_list[i])<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Faster<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">n = len(my_list)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for i in range(n):<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0process(my_list[i])<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">That <\/span><i><span style=\"font-weight: 400;\">len()<\/span><\/i><span style=\"font-weight: 400;\"> call isn\u2019t free. Cache it if you can!<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">9. Leverage Multi-threading or Multi-processing<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Python\u2019s Global Interpreter Lock (GIL) limits multi-threading with CPU-bound tasks. But you can still use it for IO-heavy ones.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For CPU-bound stuff, go with multiprocessing.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Task Type<\/b><\/td>\n<td><b>Use<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">IO-bound (e.g., web scraping)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">threading<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">CPU-bound (e.g., image processing)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">multiprocessing<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Also check <\/span><i><span style=\"font-weight: 400;\">joblib<\/span><\/i><span style=\"font-weight: 400;\"> if you\u2019re doing ML model training or parallel loops.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">10. Use PyPy If You Can<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">PyPy is a faster alternative to the standard Python interpreter. It uses JIT (Just-in-Time) compilation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You might see a 4\u201310x speedup without changing any of your code.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">More about it here:<\/span><a href=\"https:\/\/www.pypy.org\/\"> <span style=\"font-weight: 400;\">https:\/\/www.pypy.org\/<\/span><\/a><\/p>\n<h3><span style=\"font-weight: 400;\">11. Avoid Unpacking in Loops<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">This can be surprisingly expensive in tight loops.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Slower<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for key, value in my_dict.items():<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(key, value)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Faster<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">items = my_dict.items()<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for item in items:<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(item[0], item[1])<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Not always a massive gain, but helps in big loops.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">12. Use <\/span><i><span style=\"font-weight: 400;\">join(<\/span><\/i><i><span style=\"font-weight: 400;\">)<\/span><\/i><span style=\"font-weight: 400;\"> Instead of + for Strings<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">String concatenation with + creates new strings every time. That kills performance in large loops.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><i><span style=\"font-weight: 400;\"># Slower<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">result = &#8220;&#8221;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">for s in list_of_strings:<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0result += s<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"># Faster<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">result = &#8220;&#8221;.join(list_of_strings)<\/span><\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Cleaner and faster.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Table: Quick Comparison of <\/span><span style=\"font-weight: 400;\">Python Code Optimisation<\/span><span style=\"font-weight: 400;\"> Techniques<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Here\u2019s a comprehensive overview of the various <\/span><span style=\"font-weight: 400;\">Python code<\/span><span style=\"font-weight: 400;\"> optimisation techniques, their uses and the performance levels:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Optimisation Trick<\/b><\/td>\n<td><b>Performance Gain<\/b><\/td>\n<td><b>Where to Use<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">List Comprehensions<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Medium<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Loops &amp; filtering<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Generators<\/span><\/td>\n<td><span style=\"font-weight: 400;\">High<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Memory-saving loops<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">NumPy Arrays<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Very High<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Math-heavy scripts<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Caching (lru_cache)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">High<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Recursive or repeated functions<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Multiprocessing<\/span><\/td>\n<td><span style=\"font-weight: 400;\">High<\/span><\/td>\n<td><span style=\"font-weight: 400;\">CPU-bound parallel tasks<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span style=\"font-weight: 400;\">Watch More:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">PYTHON for Beginners: Learn Python Programming from Scratch (Step-by-Step)<\/span><\/p>\n<p><iframe loading=\"lazy\" title=\"PYTHON for Beginners: Learn Python Programming from Scratch (Step-by-Step) | Module 01\" src=\"https:\/\/www.youtube.com\/embed\/7zc0yz2q1vc?list=PLGnEY8uzzUsNZz0WHjmDXILGV0yAWgx_n\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><span style=\"font-weight: 400;\">PANDAS in Python | Python for Beginners<\/span><\/p>\n<p><iframe loading=\"lazy\" title=\"PANDAS in Python | Python for Beginners | Module 06\" src=\"https:\/\/www.youtube.com\/embed\/4gCYuvYHwaE?list=PLGnEY8uzzUsNZz0WHjmDXILGV0yAWgx_n\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-weight: 400;\">Final Thoughts<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">You don\u2019t need to over-optimise every single function. That\u2019s a waste of time. Focus on the areas that cause real-world pain \u2014 where the app slows down, where the user gets frustrated, or where batch jobs take hours.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Start by profiling your code. Use built-in tools. Then apply fixes like switching to generators, NumPy, or caching results.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you want to seriously upgrade your skills and learn how real companies <\/span><span style=\"font-weight: 400;\">optimise Python scripts<\/span><span style=\"font-weight: 400;\">, work with data, and build intelligent solutions \u2014 check out the full<\/span><a href=\"https:\/\/imarticus.org\/postgraduate-program-in-data-science-analytics\/\"> <b>Postgraduate Program in Data Science and Analytics by Imarticus Learning<\/b><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It\u2019s got real-world projects, solid instructors, and a focus on practical coding.\u00a0<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">FAQs<\/span><\/h3>\n<ul>\n<li aria-level=\"1\"><b>What\u2019s the first step in <\/b><b>Python code optimisation<\/b><b>?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">Start by profiling your <\/span><span style=\"font-weight: 400;\">Python code<\/span><span style=\"font-weight: 400;\"> using tools like cProfil<\/span><span style=\"font-weight: 400;\">e<\/span><span style=\"font-weight: 400;\">. Don\u2019t guess. Measure what\u2019s slow and fix that first.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Does Python run slow because it\u2019s interpreted?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">Yes and no. It\u2019s slower than compiled languages like C. But you can speed it up massively with things like NumPy, PyPy, and multiprocessing.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Is it worth rewriting <\/b><b>Python code<\/b><b> in C or Cython?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">If performance is really critical, yes. But for most cases, built-in modules, vectorisation, or JIT interpreters are enough.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Can using functions slow down <\/b><b>Python code<\/b><b>?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">Not always. But calling a function repeatedly inside a loop can add overhead. If it\u2019s something simple, inlining it might help.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>What are some good tools to <\/b><b>optimise Python scripts<\/b><b>?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">Try cProfile, line_profiler, memory_profiler, Py-Spy, and NumPy for performance. Joblib and multiprocessing help for parallelism.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>When should I not worry about optimisation?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">If the script runs once a day and takes 2 minutes, who cares? Focus only when performance affects users or dev time.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Is Python bad for large-scale applications?<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\">Nope. Big companies use Python at scale. You just need to know where the bottlenecks are and how to fix them.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is a truth known that a well-made app or website has its foundation in a well-built code. Developers have a tendency to obsess over performance. Most of your codebase probably runs just fine and doesn\u2019t impact the overall speed of your app. Especially for scripts that run on a schedule or behind the scenes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_mo_disable_npp":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[23],"tags":[5318],"class_list":["post-269094","post","type-post","status-publish","format-standard","hentry","category-analytics","tag-python-code"],"acf":[],"aioseo_notices":[],"modified_by":"Imarticus Learning","_links":{"self":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/269094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/comments?post=269094"}],"version-history":[{"count":1,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/269094\/revisions"}],"predecessor-version":[{"id":269096,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/269094\/revisions\/269096"}],"wp:attachment":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/media?parent=269094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/categories?post=269094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/tags?post=269094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}