Algorithm taking too long

C
CameronR (220 points)
0 2 4
asked Oct 30, 2020 in Programming in Python by CameronR (220 points)
I am wondering how I can improve code that is taking too long to run.

For instance, say I have any short program/function that works correctly but times out when being tested with larger test data - what should I look for or remove from my code that would help it speed up?
355 views

2 Answers

Best answer
andrea.sterbini (172780 points)
514 935 1789
answered Oct 30, 2020 by andrea.sterbini (172,780 points)
selected Oct 30, 2020 by CameronR
I suggest you use the spyder-line-profiler plugin to find what part of your code takes more time, and improve it
b
benjamin (2490 points)
0 6 21
answered Oct 30, 2020 by benjamin (2,490 points)
You should look to find a way to reduce if statements as much as possible. Avoid iterating multiple times over a list and try to do everything you can in fewer iteration as possible.

When you can use builtin function ( for example sum instead of iterating with a for loop) since they are more often than not faster than what you might be able to write.