Benchmarking Algorithms in Java
Dr Patrick Mannion
[Link]@[Link]
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 1
Overview
• Motivation for benchmarking
• Time in Java
• 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 Java
• Dates and times in Java are represented as the number of
milliseconds that have elapsed since midnight on January 1 1970 (the
“Unix Epoch”)
• 1 second = 1,000 milliseconds = 1,000,000,000 nanoseconds
• Each millisecond since the Unix Epoch has a specific timestamp
• Stored using the long type in Java (i.e. a timestamp is a large integer)
• e.g. long currentTime = [Link](); // current time in millisec
• [Link]() gives a higher resolution
• e.g. long currentTime = [Link](); // current time in nanoseconds
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
• Java 8 System class documentation:
[Link]
• Java Date and Time overview:
[Link]
• Discussion of benchmarking issues in Java (advanced material):
[Link]
Dr Patrick Mannion, Dept of Computer Science & Applied Physics 7