Change language

Python | Make the program run faster

| |

While the first rule of optimization might be "don’t do this," the second rule almost certainly says, "Don’t optimize the irrelevant." To this end, if your program is running slowly, you can start by profiling your code. More often than not, you find that a program is wasting its time on multiple hotspots, such as internal data loops. Once these locations are determined, all the necessary techniques can be used to speed up the program.

Many programmers start using Python as a language for writing simple scripts. When writing scripts, it is easy to fall into the practice of simply writing code with very little structure.

Code # 1: With this code in mind.

# abc.py

import sys

import csv

  

with open (sys.argv [ 1 ]) as f:

  for row in csv. reader (f):

# Some kind of processing

A little known fact is that code defined in the global scope is slower than code defined in a function. The difference in speed is related to the implementation of local and global variables (operations involving local operators are faster). So, just put script statements in a function to make the program run faster.

Code # 2:

# abc.py

import sys

import csv

 

def main (filename):

  with open (filename) as f:

for row in csv.reader (f):

# Some kind of processing

  

main (sys.argv [ 1 ])

The difference in speed is strong depends on the processing being performed, but 15-30% speedups are not uncommon.

Selectively exclude the access attribute —

Each use of the dot (.) operator to access attributes is expensive ... In disguise, this calls special methods like __getattribute __ () and __getattr __ () , which often result in __getattr __ () in the dictionary.

It is often possible to avoid looking for attributes by using an import form and also choosing to use related methods as shown in the code snippet below —

Code # 3:

Output:

 This p rogram runs in about 40 seconds when running on the machine. 

Code # 4: Modify compute_roots()

import math

  

def compute_roots (nums):

result = [ ]

for n in nums:

  result.append (math. sqrt (n))

return result

 
# Test Job

nums = range ( 1000000 )

for n in range ( 100 ):

  r = compute_roots (nums)

from math import sqrt

 

def compute_roots (nums):

result = []

result_append = result.append

  for n in nums:

  result_append ( sqrt (n))

return result

Exit:

 This program runs in about 29 seconds when running on the machine. 

The only difference between the two versions of the code is the exclusion of access to attributes. Instead of using math. sqrt () , the code uses sqrt () . The result.append () method is additionally placed in the local variable re
sult_append and reused in the inner loop. 
However, it should be emphasized that these changes only make sense in code that is executed frequently, such as for loops. Thus, this optimization really only makes sense in carefully selected places.

Understand the locality of variables —

As noted earlier, local variables are faster than global variables. For commonly used names, accelerations can be obtained by making these names as local as possible.

Code # 5: Modified version of compute_roots()

import math

 

def compute_roots (nums):

sqrt = math. sqrt

result = []

result_append = result.append

  for n in nums:

result_append ( sqrt (n))

return result

In this release, sqrt has been removed from the math module and placed in a local variable. This code will run for about 25 seconds (an improvement over the previous version, which took 29 seconds). This additional speedup is due to the fact that the local sqrt search is slightly faster than the global sqrt search. 
Locality arguments also apply when working in classes. In general, finding a value such as self.name will be significantly slower than accessing a local variable. Inner loops may need to hoist public attributes into a local variable, as shown in the code below.

Code # 6:

# Slow down

class SomeClass:

...

def method ( self ):

for x in s:

op ( self . value)

# Fast her

class SomeClass:

...

def method ( self ):

value = self . value

  for x in s:

op (value)

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically