- Notifications
You must be signed in to change notification settings - Fork0
Here are some amazingly useful tips to decrease execution time in python programs
License
BhargavKadali39/tips-to-decrease-executionTime-in-python
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Here are some amazingly useful tips to decrease execution time in python programs
As a beginner I admit that most of the tips given below are some of my deeds too, so this repo we will learn how to decrease the execution time.
The method I used here to calculate the time is previously explained in one of my repos which you can find by
clicking here
- The execution-time for every code given below is based on multiple runs and optimal one of all.
- Check out the other files in this repo for hands-on trial.
- Time may vary device to device.
When we try to use certain module and we know it's sole purpose is only to serve a single function.Then try importing only that single function instead of the whole thing.
syntax
from module_name import function_name1, function_name2,etc,...
Example:
from math import ceil, floori = 9.67print(ceil(i))print(floor(i))output: 10 9Execution time: 0.008982419967651367
import mathi = 9.67print(math.ceil(i))print(math.floor(i))Execution time: 0.01605224609375see the difference, amazing right
In one of my previous repos about differentswapping techniques I stated to use
a = 10b = 20a, b = b, aprint(a,b)Execution time is : 0.014656782150268555
Instead of the below code
x = 10y = 20temp = xx = yy = tempprint(x,y)Output: 20 10Execution time is : 0.014772176742553711Might be closer but worthwhile when running in a loop.
This is the most common mistake made by most of us, that is using while-loop even tho the job could be done with a for-loop.
for i in range(10):print(i)if i == 9: print('over') output:0123456789overExecution time is : 0.05827617645263672
i=0while True: print(i) i += 1 if i == 10: print('over') break output:0123456789overExecution time is : 0.07421302795410156
The old saying goesLittle efforts can make a huge impact.
As a programmer I sayEvery bit counts ;)
About
Here are some amazingly useful tips to decrease execution time in python programs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.