DEV Community

Cover image for 10 Python programming optimisation techniques.

10 Python programming optimisation techniques.

James Ononiwu on August 18, 2024

Optimised code is essential because it directly impacts the efficiency, performance, and scalability of software. Well-written code runs faster, co...
Collapse
 
michaelmior profile image
Michael Mior

While it's certainly good practice to keep some things "private" that are not intended to be used by external callers, this does nothing for speed or efficiency. Prefixing with an underscore as in the example is really just a convention anyway and doesn't prevent calling the function.

Collapse
 
jamesbright profile image
James Ononiwu

Thanks for the feedback.

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield • Edited

Moreover, optimised code is often more straightforward to maintain and extend.

Actually, optimized code is harder to mantain. That's why it is much better to use an appropriate algorithm than use, say, arrays instead of lists. Moreover, the use of these techniques before improving the algorithm in use would be an example of premature optimization.

Collapse
 
norweeg profile image
Brennen Raimer • Edited

consider lazy evaluation (map, filter, reduce, and the entire itertoolsmodule). I have often seen folks comprehend a large list into memory for a loop only to break the loop early meaning all members of the comprehended list with indices higher than the member where the list was broken were wasted CPU time and memory. I have also seen folks comprehend an original dataset into a second, separate variable (possibly for clarity's sake?) to use once and then never again but the GC never collects it because a variable still refers to it. It would have been better to either assign the second variable as a map of the original data variable or simply iterate directly over the comprehended list without assigning a variable name to it so that the GC cleans it up immediately after the loop is complete rather than having it stick around in memory for no reason.

tl;dr there is a time and place for mappings vs comprehensions. Both have pros and cons. Please consider why and how you are using comprehended variables!

Collapse
 
jamesbright profile image
James Ononiwu

Yes that’s right, thanks for the insight.

Collapse
 
roshan_khan_28 profile image
roshan khan

loops are the most unoptimized when it comes to python. many devs overlook this.
great read james!

Collapse
 
denys_bochko profile image
Denys Bochko

I can tell you that the loop problem is dev specific, not Python. You need to design good logic or program flow to have optimized loops. I've seen 2-3 loops in places where changing the flow will leave just 1 in place

Collapse
 
ddebajyati profile image
Debajyati Dey

Agreed!

Collapse
 
jamesbright profile image
James Ononiwu

@roshan_khan_28 yes that’s right, thanks for the feedback.

Collapse
 
bsutor profile image
Robert Sutor

Interesting, and similar advice was given over 20 years for other languages. In any case, keep in mind Knuth's advice:

“The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.”

stackify.com/premature-optimizatio...

Collapse
 
thumbone profile image
Bernd Wechner • Edited

Section 10 Optimizing Loops, you introduce numpy without a single mention of it. I find that a tad surprising and would fix it. Numpy is indeed great for such operations as you're illustrated but it is not a small library, nor the only way to do that (jax is another example), what you are introducing here is literally a suggestion to use numpy for efficient processing of large data sets which is great advice, but that is how it should be introduce IMHO - it's not a Python thing so much as a numpy thing.

Collapse
 
jamesbright profile image
James Ononiwu • Edited

Thanks for the notice, I have added some explanation about numpy.

Collapse
 
juanitomint profile image
Juan Ignacio Borda

While short logs could seems as good idea from the pov of an SRE it would be better to use a logger with configurable format so you can not only have control over format but also over log level
This increases application observability, log readability and shorten debug times!

Collapse
 
jamesbright profile image
James Ononiwu

@juanitomint Thank you for the addition.

Collapse
 
yaseraramesh profile image
Yaser Aramesh

Thanks a lot 🪴

Collapse
 
okabarack profile image
Barack Okaka Obama

I guess I would have to bookmark this now because some of my code are so memory tasking and would immensely benefit from these tips.

Collapse
 
ddebajyati profile image
Debajyati Dey

Good Article! Insightful and loved it!

Collapse
 
jamesbright profile image
James Ononiwu

Thanks @ddebajyati, I’m glad you enjoyed it.

Collapse
 
codemee profile image
codemee

I think the memory mapped file does not speed up the code. It just let you treat a file as a bytearray in memory . It still needs disk I/O to read the data in a file.

Collapse
 
juanitomint profile image
Juan Ignacio Borda

Yeah maybe it's a good strategy for allocate that time at startup do frequently accessed files 🤔

Collapse
 
sukhankar profile image
Sukhankar • Edited

is the python going to be the future ???
OR
google doesn't need the python developers???

Collapse
 
stokry profile image
Stokry

Nice!

Collapse
 
martinbaun profile image
Martin Baun

Great list, James! Avoiding unnecessary work is another great one ;)

Collapse
 
shardz profile image
SHARDZ

Very helpful

Collapse
 
sc0v0ne profile image
sc0v0ne

Very good post, thanks.

Collapse
 
jamesbright profile image
James Ononiwu

Thanks @sc0v0ne, glad you liked it.

Collapse
 
anna_lapushner profile image
anna lapushner

This is great! I especially find useful your points with regards to memory and loops. Thank you....
Best, Anna

Collapse
 
rbanffy profile image
Ricardo Bánffy • Edited

I don't think the mmap example is a great one - where mmap really shines is when treating the file as a large bytes object (think a array of struct.packs) you can then access by index.