Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Here are some amazingly useful tips to decrease execution time in python programs

License

NotificationsYou must be signed in to change notification settings

BhargavKadali39/tips-to-decrease-executionTime-in-python

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

points to be noted beforehand

  • 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.

do not import the whole module

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  9

Execution time: 0.008982419967651367

import mathi = 9.67print(math.ceil(i))print(math.floor(i))

Execution time: 0.01605224609375see the difference, amazing right

no temp variables

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 10

Execution time is : 0.014772176742553711Might be closer but worthwhile when running in a loop.

for loop for the win

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.

using for-loop

for i in range(10):print(i)if i == 9:    print('over')    output:0123456789over

Execution time is : 0.05827617645263672

using while-loop

i=0while True:    print(i)    i += 1    if i == 10:        print('over')        break        output:0123456789over

Execution 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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp