Benchmarking Algorithms with TheAlgorithms Java Harness
Learn how to use TheAlgorithms Java benchmark harness to time algorithms, understand its reflection‑based mechanism, and avoid common pitfalls.
11 Apr 2026, 14:56 UTC

To get a quick timing measurement for any algorithm in the TheAlgorithms/Java repository, run the built‑in harness from the command line:
java -cp target/classes com.thealgorithms.Main AlgorithmClass arraySize [repetitions]
Replace AlgorithmClass with the fully‑qualified name of the class that implements the Algorithm interface (e.g., Sorting.quickSort), arraySize with the length of the random integer array you want to test, and optionally repetitions with how many times the algorithm should be executed for averaging.
How the harness works
The entry point is the Main class (com.thealgorithms.Main). Its public static void main(String[] args) method:
- Parses the first argument as the algorithm name.
- Uses Java reflection to locate the class implementing
com.thealgorithms.interfaces.Algorithmin thecom.thealgorithmspackage. - Instantiates that class and prepares a random
int[]of the requested size. - For each repetition (default 1) it:
- Records
System.nanoTime(). - Calls
algorithm.execute(input). - Records the end time and accumulates the elapsed nanoseconds.
- After the run, checks correctness by comparing the result with
Arrays.sorton a copy of the original array. - Finally prints the average runtime in milliseconds and a “PASS/FAIL” correctness message.
Worked example
Assuming you have cloned the repository and compiled it with Maven:
# From the repository root
mvn clean compile
# Run QuickSort on 10 000 integers, averaged over 5 runs
java -cp target/classes com.thealgorithms.Main Sorting.quickSort 10000 5
The command will output something like:
Average time: 2.34 ms
Result: PASS
(Exact numbers will vary; the harness does not guarantee any specific value.)
Limits of the harness
- Interface constraint: Only classes that implement the single‑method
Algorithm.execute(int[])interface can be benchmarked directly. Algorithms that need extra parameters (e.g., graph algorithms requiring an adjacency list) must be wrapped or are excluded. - JVM warm‑up and GC: The timing includes class loading, JIT compilation, and garbage‑collection overhead. For very small input sizes the measured time may be dominated by these effects rather than the algorithm’s intrinsic cost.
- Single‑threaded: The harness runs sequentially; concurrent execution from multiple threads is not supported and may produce incorrect results due to shared mutable state.
Common mistakes and how to avoid them
- Missing classpath: If
target/classesis not on the classpath, you will seeClassNotFoundException. Always include-cp target/classes(or the equivalent Mavenexec:javaplugin). - Incorrect algorithm name: The name must match the exact fully‑qualified class name, case‑sensitive. A typo leads to a reflection error like
NoSuchMethodExceptionorInvocationTargetException. - Reusing mutable input: The harness creates a fresh random array for each repetition, but if you modify the harness to reuse the same array, the array will be sorted after the first run, causing artificially low times. Keep the input generation inside the timing loop or reset the array before each iteration.
Practical verification steps
- Clone the repository:
git clone https://github.com/TheAlgorithms/Java.git - Check out the latest
mainbranch and build:mvn clean compile - Run a known algorithm, for example merge sort on 5 000 elements with three repetitions:
- Observe the printed average time and a “PASS” correctness line.
- Vary the
arraySizeorrepetitionsand confirm that the reported time changes in a predictable direction (larger size → higher time, more repetitions → similar average but tighter confidence). - Supply a non‑existent algorithm name (e.g.,
java -cp target/classes com.thealgorithms.Main NonExistent.Algo 1000 1) and verify that the harness prints a clear error message about class not found.
java -cp target/classes com.thealgorithms.Main Sorting.mergeSort 5000 3
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.