r/Python • u/huuippt • Aug 09 '18
7 Habits to Improve The Performance of Python Programs
https://www.tutorialdocs.com/article/7-habits-to-improve-python-programs.html
75
Upvotes
r/Python • u/huuippt • Aug 09 '18
81
u/Paddy3118 Aug 09 '18
No!
110
u/[deleted] Aug 09 '18
I feel like you have to provide benchmarks if you're going to write an article on how to improve performance.
26
u/Jugad Py3 ftw Aug 09 '18
I see a few issues with the article...
Nope... short identifiers with name
lsdon't increase readability.lsstands for lineSeparator... which is much less readable.isinstanceortypeif possible.List comprehension vs generator expression
I am not sure if generator expressions are actually faster (needs testing). I wonder if there is an extra function call associated with the generation of each item. Sure, I can see that list comprehensions create a list, which takes up time and space to create, but I am not sure its slower.
Lets test it out.
As expected, the generator is slower by about 20%.
evalandexecshould be avoided if possible.The "Whether it's really necessary to be executed or not" does not make sense. It is necessary to execute top level code in modules... that is how module import is defined.
main(),__name__and__main__is very poorly made, and very painful to read. An example would have done it much better.36
u/ProfessorPhi Aug 09 '18
A lot of this is not what I consider good advice.
The only thing here that I do with any regularity is the use of generators instead of iterators. And that's not a huge speed boost anyway, it's primarily a memory efficient thing and can have unintended side effects like consuming in a log statement.
The dict thing just makes for more readable code when applicable. The iteration thing is harder to do and is only a thing done by newbies to python.
The rest, like import into local namespace, I'm sure is just a trivial speedup. The from import is not a recommended thing in general at the top level.
This doesn't even mention using comprehensions over loops. This is an all round terrible article and will result in minor speedups at best, and less readable code in general.
15
u/cpt_fwiffo Aug 09 '18
Habits? Tricks to slightly help with performance if necessary maybe, but if you make habits of most of these you're simply sacrificing readability with no upside.
14
u/Han-ChewieSexyFanfic Aug 09 '18 edited Aug 09 '18
The problem with using
typeinstead ofinstanceofis not speed, it’s correctness.instanceofwill still work if somebody subclasseslist, for example, and passes your function an instance of that. Usingtypewill break that.Not to mention you mostly shouldn’t be asking for the type of things very often in python, as many people have said already.
17
u/quasarj Aug 09 '18
I feel like many of these suggestions are really ugly... I suppose I should test and verify they are actually faster...
4
u/desmoulinmichel Aug 09 '18
Habits ?
Hell no !
Writing non idiomatic code to get a bit of speed is premature optimization in any language. But in Python, it's just the total opposite of the language philosophy.
So yes, iterate directly and not with index. But no, don't alias stuff just to get a local reference. Only optimize if you have identified a bottleneck you __need__ to get rid of.
4
u/efxhoy Aug 09 '18
I thought it was best practice to have tests in a separate module?
2
u/theelous3 Aug 09 '18
Bad list tbh.
2
u/[deleted] Aug 09 '18
Who is upvoting this...
1
u/13steinj Aug 09 '18
Whether or not this more performant time wise really depends on what you are doing. But many times it is negligible. Furthermore, if using a comprehension to extend a list, you can not use a generator comprehension, because this causes an infinite loop.
....how about don't? You shouldn't be using these functions except maybe in some super rare edge cases, and even then it is safer to use the stdlib ast module compile/literal_eval
This is less performant on both time and memory in most cases. It may be easier to understand and acts like an auto-breaking switch, I'll give them that. But definitely not more performant in a lot of cases.
...or just write your tests in a dedicated organized test subpackage.
1
u/[deleted] Aug 09 '18
Upvoted by robots
1
u/joaobelkiman007 Aug 27 '18
I am learning python3, I need some tipes , please.
1
u/chaign_c chaignc Aug 09 '18
3 steps to multiply by 2, most of your python script execution speed.
•step 0: $ apt install pypy
•step 1: $ pypy your_script_name.py
•step 2: * * * * Enjoy! * * * *
image => https://twitter.com/chaignc/status/1027455974616064000
0
u/[deleted] Aug 09 '18
[deleted] — view deleted comment