+ 2

What are the tricks to make Python 'for loops' much faster?

What are the tricks to make Python 'for loops' much faster?

25th Mar 2019, 9:38 PM
Yusuf
Yusuf - avatar
5 Antworten
+ 2
Dyf Dyf Thank you I knew it. I am looking for python specific solutions.
26th Mar 2019, 6:29 AM
Yusuf
Yusuf - avatar
+ 1
I don't know about python nor it syntax but in any language, to optimize your code you shouldn't put any function in the declaration of the loop. Here is an exemple : Prefer => size = array.length For(i=0 ; i < size ; i++) To => For(i=0 ; i < array.length ; i++) If you put a function inside of the loop declaration, it gonna be called each iteration, the result wont be stored by itself.
26th Mar 2019, 6:26 AM
Dyf Dyf
Dyf Dyf - avatar
+ 1
Hope someone will be of more help then :) The question is actually quite interesting (even for a non python dev as myself)
26th Mar 2019, 6:34 AM
Dyf Dyf
Dyf Dyf - avatar
+ 1
Firstly, I would suggest to avoid 'for loops' as much as possible and use list comprehension instead. In case we have to use 'for loops', found this article on optimizing the loop. Have tested the codes & it works perfectly https://nyu-cds.github.io/JUMP_LINK__&&__python__&&__JUMP_LINK-performance-tips/08-loops/
27th Mar 2019, 10:03 AM
Bishu Giri
Bishu Giri - avatar
+ 1
Bishu Giri Thank you, but I could not able to use list comprehension for every problem. I have read this article already, thank you again.
27th Mar 2019, 1:08 PM
Yusuf
Yusuf - avatar