SlideShare a Scribd company logo
1 of 17
Download to read offline
Loops in Python –
Comparison and
Performance
This content was originally published at:
https://www.blog.duomly.com/loops-in-python-comparison-and-performance/

***
Python is one of the most popular
programming languages today. It’s an
interpreted and high-level language with
elegant and readable syntax. However,
Python is generally significantly slower
than Java, C#, and especially C, C++, or
Fortran. Sometimes performance issues and
bottlenecks might seriously affect the
usability of applications.
Fortunately, in most cases, there are
solutions to improve the performance of
Python programs. There are choices
developers can take to improve the speed
of their code. For example, the general
advice is to use optimized Python built-
in or third-party routines, usually
written in C or Cython. Besides, it’s
faster to work with local variables than
with globals, so it’s a good practice to
copy a global variable to a local before
the loop. And so on.
Finally, there’s always the possibility
to write own Python functions in C, C++,
or Cython, call them from the application
and replace Python bottleneck routines.
But this is usually an extreme solution
and is rarely necessary for practice.
Often performance issues arise when using
Python loops, especially with a large
number of iterations. There is a number
of useful tricks to improve your code and
make it run faster, but that’s beyond the
scope here.
This article compares the performance of
several approaches when summing two
sequences element-wise:
• Using the while loop
• Using the for loop
• Using the for loop with list
comprehensions
• Using the third-party library numpy
However, performance isn’t the only
concern when developing software.
Moreover, according to Donald Knuth in
The Art of Computer Programming,
“premature optimization is the root of
all evil (or at least most of it) in
programming”. After all, “readability
counts”, as stated in the Zen of Python
by Tim Peters.
Problem Statement
We’ll try to sum two sequences element-
wise. In other words, we’ll take two
sequences (list or arrays) of the same
size and create the third one with the
elements obtained by adding the
corresponding elements from the inputs.
Preparation
We’ll import Python built-in package
random and generate a list r that
contains 100.000 pseudo-random numbers
from 0 to 99 (inclusive):
import random
r = [random.randrange(100) for _ in
range(100_000)]
We’ll also use the third-party
package numpy, so let’s import it:
import numpy as np
And we’re ready to go!
Simple Loops
Let’s first see some simple Python loops
in action.
Using Pure Python
We’ll start with two lists with 1.000
elements each. The integer variable n
represents the length of each list. The
lists x and y are obtained by randomly
choosing n elements from r:
n = 1_000
x, y = random.sample(r, n), random.sample(r, n)
Let’s see what is the time needed to get
a new list z with n elements, each of
which is the sum of the corresponding
elements from x and y.
We’ll test the performance of the while
loop first:
%%timeit
i, z = 0, []
while i < n:
z.append(x[i] + y[i])
i += 1
The output is:
160 µs ± 1.44 µs per loop (mean ± std.
dev. of 7 runs, 10000 loops each)
Please, note that the output
of timeit depends on many factors and
might be different each time.
The for loop in Python is better
optimized for the cases like this, that
is to iterate over collections,
iterators, generators, and so on. Let’s
see how it works:
%%timeit
z = []
for i in range(n):
z.append(x[i] + y[i])
The output is:
122 µs ± 188 ns per loop (mean ± std.
dev. of 7 runs, 10000 loops each)
In this case, the for loop is faster, but
also more elegant compared to while.
List comprehensions are very similar to
the ordinary for loops. They are suitable
for simple cases (like this one).
In addition to being more compact, they
are usually slightly faster since some
overhead is removed:
%%timeit
z = [x[i] + y[i] for i in range(n)
The output is:
87.2 µs ± 490 ns per loop (mean ± std.
dev. of 7 runs, 10000 loops each)
Please, have in mind that you can’t apply
list comprehensions in all cases when you
need loops. Some more complex situations
require the ordinary for or even while
loops.
Using Python with NumPy
numpy is a third-party Python library
often used for numerical computations.
It’s especially suitable to manipulate
arrays. It offers a number of useful
routines to work with arrays, but also
allows writing compact and elegant code
without loops.
Actually, the loops, as well as other
performance-critical operations, are
implemented in numpy on the lower level.
That allows numpy routines to be much
faster compared to pure Python code. One
more advantage is the way numpy handles
variables and types.
Let’s first use the lists of Python
integers x and y to create
corresponding numpy arrays of 64-bit
integers:
x_, y_ = np.array(x, dtype=np.int64), np.array(y,
dtype=np.int64)
Summing two numpy arrays x_ and y_
element-wise is as easy as x_ + y_. But
let’s check the performance:
%%timeit
z = x_ + y_
The output is:
1.03 µs ± 5.09 ns per loop (mean ± std.
dev. of 7 runs, 1000000 loops each)
That’s almost 85 times faster than when
we used list comprehensions.
And the code is extremely simple and
elegant. numpy arrays can be a much better
choice for working with large arrays.
Performance benefits are generally
greater when data is bigger.
It might be even better. If we’re OK with
using 32-bit integers instead of 64-bit,
we can save both memory and time in some
cases:
x_, y_ = np.array(x, dtype=np.int32), np.array(y,
dtype=np.int32)
We can add these two arrays as before:
%%timeit
z = x_ + y_
The output is:
814 ns ± 5.8 ns per loop (mean ± std.
dev. of 7 runs, 1000000 loops each)
The results obtained when n is larger,
that is 10_000 and 100_000 are shown in
the table below. They show the same
relationships, as in this case, with even
higher performance boost when using numpy.
Nested Loops
Let’s now compare the nested Python
loops.
Using Pure Python
We’ll work with two lists called x and y
again. Each of them will contain 100
inner lists with 1.000 pseudo-random
integer elements. Thus, x and y will
actually represent the matrices with 100
rows and 1.000 columns:
m, n = 100, 1_000
x = [random.sample(r, n) for _ in range(m)]
y = [random.sample(r, n) for _ in range(m)]
Let’s see the performance of adding them
using two nested while loops:
%%timeit
i, z = 0, []
while i < m:
j, z_ = 0, []
while j < n:
z_.append(x[i][j] + y[i][j])
j += 1
z.append(z_)
i += 1
The output is:
19.7 ms ± 271 µs per loop (mean ± std.
dev. of 7 runs, 100 loops each Again, we
can get some performance improvement with
the nested for loops:
%%timeit
z = []
for i in range(m):
z_ = []
for j in range(n):
z_.append(x[i][j] + y[i][j])
z.append(z_)
The output is:
16.4 ms ± 303 µs per loop (mean ± std.
dev. of 7 runs, 100 loops each) In some
cases, the nested for loops can be used
with lists comprehensions, enabling an
additional benefit:
%%timeit
z = [[x[i][j] + y[i][j] for j in range(n)] for i
in range(m)]
The output is:
12.1 ms ± 99.4 µs per loop (mean ± std.
dev. of 7 runs, 100 loops each)
We can see that in the case of nested
loops, list comprehensions are faster
than the ordinary for loops, which are
faster than while.
In this case, we have 100.000 (100×1.000)
integer elements in each list. This
example is slightly slower than the one
with 100.000 elements and a single loop.
That’s the conclusion for all three
approaches (list comprehensions, ordinary
for, and while loops).
Using Python with NumPy
numpy is great to work with multi-
dimensional arrays. Let’s use x and y to
create corresponding numpy arrays of 64-
bit integers:
x_, y_ = np.array(x, dtype=np.int64), np.array(y,
dtype=np.int64)
And let’s check the performance:
%%timeit
z = x_ + y_
The output is:
69.9 µs ± 909 ns per loop (mean ± std.
dev. of 7 runs, 10000 loops each) That’s
about 173 times faster than list
comprehensions. But it might be even
faster if we use 32-bit integers:
x_, y_ = np.array(x, dtype=np.int32), np.array(y,
dtype=np.int32)
The performance check is done as before:
%%timeit
z = x_ + y_
The output is:
34.3 µs ± 44.6 ns per loop (mean ± std.
dev. of 7 runs, 10000 loops each) which
is twice faster than with 64-bit
integers.
Results Summary
This table summarizes the obtained
results:
Number of
elements,
m × n
1
×
1K
1
×
10K
1
×
100K
100
×
1K
Python while
160
µs
1.66 ms 17.0 ms 19.7 ms
Python for
122
µs
1.24 ms 13.4 ms 16.4 ms
Python for with
list
comprehension
87.2
µs
894 µs 8.92 ms 12.1 ms
numpy with 64-
bit integers
1.03
µs
6.36 µs 71.9 µs 69.9 µs
numpy with 32-
bit integers
814
ns
2.87 µs 34.1 µs 34.3 µs
Conclusions of python faster for loop
This article compares the performance of
Python loops when adding two lists or
arrays element-wise.
The results show that list comprehensions
were faster than the ordinary for loop,
which was faster than the while loop. The
simple loops were slightly faster than
the nested loops in all three cases.
numpy offers the routines and operators
that can substantially reduce the amount
of code and increase the speed of
execution. It’s especially useful when
working with single- and multi-
dimensional arrays.
Please, have in mind that the conclusions
or relations among the results obtained
here are not applicable, valid, or useful
in all cases! They are presented for
illustration. The proper way to handle
inefficiencies is to discover the
bottlenecks and perform own tests.
Table of contents:
• Problem Statement
• Preparation
• Simple Loops
• Nested Loops
• Results Summary
• Conclusions

More Related Content

What's hot

Intel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance ProgrammingIntel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance Programming
Brian Gesiak
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
Sri Ambati
 

What's hot (20)

NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
 
NUMPY
NUMPY NUMPY
NUMPY
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
 
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
Rajat Monga at AI Frontiers: Deep Learning with TensorFlowRajat Monga at AI Frontiers: Deep Learning with TensorFlow
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
 
Intel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance ProgrammingIntel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance Programming
 
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
 
Natural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usageNatural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usage
 
機械学習によるデータ分析 実践編
機械学習によるデータ分析 実践編機械学習によるデータ分析 実践編
機械学習によるデータ分析 実践編
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
Introduction to Tensorflow
Introduction to TensorflowIntroduction to Tensorflow
Introduction to Tensorflow
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
 
Introduction to Machine Learning with TensorFlow
Introduction to Machine Learning with TensorFlowIntroduction to Machine Learning with TensorFlow
Introduction to Machine Learning with TensorFlow
 
Howto argparse
Howto argparseHowto argparse
Howto argparse
 

Similar to Python faster for loop

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
KarthikVijay59
 

Similar to Python faster for loop (20)

Writing Faster Python 3
Writing Faster Python 3Writing Faster Python 3
Writing Faster Python 3
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
 
Complex models in ecology: challenges and solutions
Complex models in ecology: challenges and solutionsComplex models in ecology: challenges and solutions
Complex models in ecology: challenges and solutions
 
Lec1
Lec1Lec1
Lec1
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
[PR12] PR-036 Learning to Remember Rare Events
[PR12] PR-036 Learning to Remember Rare Events[PR12] PR-036 Learning to Remember Rare Events
[PR12] PR-036 Learning to Remember Rare Events
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
 

More from 💾 Radek Fabisiak

More from 💾 Radek Fabisiak (9)

Css border examples
Css border examplesCss border examples
Css border examples
 
Html projects for beginners
Html projects for beginnersHtml projects for beginners
Html projects for beginners
 
Javascript for loop
Javascript for loopJavascript for loop
Javascript for loop
 
Css background image
Css background imageCss background image
Css background image
 
Node js projects
Node js projectsNode js projects
Node js projects
 
Button hover effects
Button hover effectsButton hover effects
Button hover effects
 
React projects for beginners
React projects for beginnersReact projects for beginners
React projects for beginners
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 
The best programming languages for blockchain
The best programming languages for blockchainThe best programming languages for blockchain
The best programming languages for blockchain
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 

Python faster for loop

  • 1. Loops in Python – Comparison and Performance
  • 2. This content was originally published at: https://www.blog.duomly.com/loops-in-python-comparison-and-performance/ *** Python is one of the most popular programming languages today. It’s an interpreted and high-level language with elegant and readable syntax. However, Python is generally significantly slower than Java, C#, and especially C, C++, or Fortran. Sometimes performance issues and bottlenecks might seriously affect the usability of applications. Fortunately, in most cases, there are solutions to improve the performance of Python programs. There are choices developers can take to improve the speed of their code. For example, the general advice is to use optimized Python built- in or third-party routines, usually written in C or Cython. Besides, it’s faster to work with local variables than with globals, so it’s a good practice to copy a global variable to a local before the loop. And so on. Finally, there’s always the possibility to write own Python functions in C, C++, or Cython, call them from the application and replace Python bottleneck routines.
  • 3. But this is usually an extreme solution and is rarely necessary for practice. Often performance issues arise when using Python loops, especially with a large number of iterations. There is a number of useful tricks to improve your code and make it run faster, but that’s beyond the scope here. This article compares the performance of several approaches when summing two sequences element-wise: • Using the while loop • Using the for loop • Using the for loop with list comprehensions • Using the third-party library numpy However, performance isn’t the only concern when developing software. Moreover, according to Donald Knuth in The Art of Computer Programming, “premature optimization is the root of all evil (or at least most of it) in programming”. After all, “readability counts”, as stated in the Zen of Python by Tim Peters.
  • 4. Problem Statement We’ll try to sum two sequences element- wise. In other words, we’ll take two sequences (list or arrays) of the same size and create the third one with the elements obtained by adding the corresponding elements from the inputs.
  • 5. Preparation We’ll import Python built-in package random and generate a list r that contains 100.000 pseudo-random numbers from 0 to 99 (inclusive): import random r = [random.randrange(100) for _ in range(100_000)] We’ll also use the third-party package numpy, so let’s import it: import numpy as np And we’re ready to go!
  • 6. Simple Loops Let’s first see some simple Python loops in action. Using Pure Python We’ll start with two lists with 1.000 elements each. The integer variable n represents the length of each list. The lists x and y are obtained by randomly choosing n elements from r: n = 1_000 x, y = random.sample(r, n), random.sample(r, n) Let’s see what is the time needed to get a new list z with n elements, each of which is the sum of the corresponding elements from x and y. We’ll test the performance of the while loop first: %%timeit i, z = 0, [] while i < n: z.append(x[i] + y[i]) i += 1 The output is: 160 µs ± 1.44 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
  • 7. Please, note that the output of timeit depends on many factors and might be different each time. The for loop in Python is better optimized for the cases like this, that is to iterate over collections, iterators, generators, and so on. Let’s see how it works: %%timeit z = [] for i in range(n): z.append(x[i] + y[i]) The output is: 122 µs ± 188 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) In this case, the for loop is faster, but also more elegant compared to while. List comprehensions are very similar to the ordinary for loops. They are suitable for simple cases (like this one). In addition to being more compact, they are usually slightly faster since some overhead is removed: %%timeit z = [x[i] + y[i] for i in range(n) The output is:
  • 8. 87.2 µs ± 490 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) Please, have in mind that you can’t apply list comprehensions in all cases when you need loops. Some more complex situations require the ordinary for or even while loops. Using Python with NumPy numpy is a third-party Python library often used for numerical computations. It’s especially suitable to manipulate arrays. It offers a number of useful routines to work with arrays, but also allows writing compact and elegant code without loops. Actually, the loops, as well as other performance-critical operations, are implemented in numpy on the lower level. That allows numpy routines to be much faster compared to pure Python code. One more advantage is the way numpy handles variables and types. Let’s first use the lists of Python integers x and y to create corresponding numpy arrays of 64-bit integers:
  • 9. x_, y_ = np.array(x, dtype=np.int64), np.array(y, dtype=np.int64) Summing two numpy arrays x_ and y_ element-wise is as easy as x_ + y_. But let’s check the performance: %%timeit z = x_ + y_ The output is: 1.03 µs ± 5.09 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) That’s almost 85 times faster than when we used list comprehensions. And the code is extremely simple and elegant. numpy arrays can be a much better choice for working with large arrays. Performance benefits are generally greater when data is bigger. It might be even better. If we’re OK with using 32-bit integers instead of 64-bit, we can save both memory and time in some cases: x_, y_ = np.array(x, dtype=np.int32), np.array(y, dtype=np.int32) We can add these two arrays as before: %%timeit z = x_ + y_
  • 10. The output is: 814 ns ± 5.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) The results obtained when n is larger, that is 10_000 and 100_000 are shown in the table below. They show the same relationships, as in this case, with even higher performance boost when using numpy.
  • 11. Nested Loops Let’s now compare the nested Python loops. Using Pure Python We’ll work with two lists called x and y again. Each of them will contain 100 inner lists with 1.000 pseudo-random integer elements. Thus, x and y will actually represent the matrices with 100 rows and 1.000 columns: m, n = 100, 1_000 x = [random.sample(r, n) for _ in range(m)] y = [random.sample(r, n) for _ in range(m)] Let’s see the performance of adding them using two nested while loops: %%timeit i, z = 0, [] while i < m: j, z_ = 0, [] while j < n: z_.append(x[i][j] + y[i][j]) j += 1 z.append(z_) i += 1 The output is:
  • 12. 19.7 ms ± 271 µs per loop (mean ± std. dev. of 7 runs, 100 loops each Again, we can get some performance improvement with the nested for loops: %%timeit z = [] for i in range(m): z_ = [] for j in range(n): z_.append(x[i][j] + y[i][j]) z.append(z_) The output is: 16.4 ms ± 303 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In some cases, the nested for loops can be used with lists comprehensions, enabling an additional benefit: %%timeit z = [[x[i][j] + y[i][j] for j in range(n)] for i in range(m)] The output is: 12.1 ms ± 99.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) We can see that in the case of nested loops, list comprehensions are faster than the ordinary for loops, which are faster than while.
  • 13. In this case, we have 100.000 (100×1.000) integer elements in each list. This example is slightly slower than the one with 100.000 elements and a single loop. That’s the conclusion for all three approaches (list comprehensions, ordinary for, and while loops). Using Python with NumPy numpy is great to work with multi- dimensional arrays. Let’s use x and y to create corresponding numpy arrays of 64- bit integers: x_, y_ = np.array(x, dtype=np.int64), np.array(y, dtype=np.int64) And let’s check the performance: %%timeit z = x_ + y_ The output is: 69.9 µs ± 909 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) That’s about 173 times faster than list comprehensions. But it might be even faster if we use 32-bit integers: x_, y_ = np.array(x, dtype=np.int32), np.array(y, dtype=np.int32)
  • 14. The performance check is done as before: %%timeit z = x_ + y_ The output is: 34.3 µs ± 44.6 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) which is twice faster than with 64-bit integers. Results Summary This table summarizes the obtained results: Number of elements, m × n 1 × 1K 1 × 10K 1 × 100K 100 × 1K Python while 160 µs 1.66 ms 17.0 ms 19.7 ms Python for 122 µs 1.24 ms 13.4 ms 16.4 ms Python for with list comprehension 87.2 µs 894 µs 8.92 ms 12.1 ms numpy with 64- bit integers 1.03 µs 6.36 µs 71.9 µs 69.9 µs
  • 15. numpy with 32- bit integers 814 ns 2.87 µs 34.1 µs 34.3 µs
  • 16. Conclusions of python faster for loop This article compares the performance of Python loops when adding two lists or arrays element-wise. The results show that list comprehensions were faster than the ordinary for loop, which was faster than the while loop. The simple loops were slightly faster than the nested loops in all three cases. numpy offers the routines and operators that can substantially reduce the amount of code and increase the speed of execution. It’s especially useful when working with single- and multi- dimensional arrays. Please, have in mind that the conclusions or relations among the results obtained here are not applicable, valid, or useful in all cases! They are presented for illustration. The proper way to handle inefficiencies is to discover the bottlenecks and perform own tests.
  • 17. Table of contents: • Problem Statement • Preparation • Simple Loops • Nested Loops • Results Summary • Conclusions