Benchmarking Algorithms in Python
Dr Patrick Mannion
[email protected]
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 1
Overview
• Motivation for benchmarking
• Time in Python
• Benchmarking a single run
• Benchmarking multiple statistical runs
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 2
Motivation for benchmarking
• Benchmarking or a posteriori analysis is an empirical method to
compare the relative performance of algorithm implementations
• Experimental (e.g. running time) data may be used to validate
theoretical or a priori analysis of algorithms
• Various hardware and software factors such system architecture, CPU
design, choice of Operating System, background processes, energy
saving and performance enhancing technologies etc. can affect
running time
• Therefore it is prudent to conduct multiple statistical runs using the
same experimental setup, to ensure that your set of benchmarks are
representative of the performance expected by an “average” user
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 3
Time in Python
• Dates and times in Python are represented as the number of seconds
that have elapsed since midnight on January 1st 1970 (the “Unix
Epoch”)
• Each second since the Unix Epoch has a specific timestamp
• Can import the time module in Python to work with dates and times
• e.g. start_time = time.time() # gets the current time in seconds
• e.g. 1555001605 is Thursday, 11 April 2019 16:53:25 in GMT
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 4
Benchmarking a single run
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 5
Benchmarking multiple statistical runs
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 6
Useful references
• Python 3 time documentation:
https://docs.python.org/3/library/time.html
• Python Date and Time overview:
https://www.tutorialspoint.com/python/python_date_time.htm
• Discussions of benchmarking issues in Python (advanced material):
https://www.peterbe.com/plog/how-to-do-performance-micro-
benchmarks-in-python
https://www.blog.pythonlibrary.org/2016/05/24/python-101-an-
intro-to-benchmarking-your-code/
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 7