My implementation of the algorithms presented in Aditya Bhargava: Grokking Algorithms, 2nd Ed.
To clean up and setup target directories enter
mvn cleanTo format the code using Prettier and to run the tests, enter
mvn testThe module benchmarks uses the Java Microbenchmark Harness JMH to compare the performance of different search algorithm implementations.
Before starting the benchmarks, compile the algorithms package:
mvn clean verifyIf you omit this step, the benchmark might measure an outdated package of algorithms.
To run the benchmarks as configured by the code annotations, enter
java -jar ./benchmarks/target/benchmarks.jar -rff benchmarks.json -rf jsonThen you can visualize the results by uploading the ./benchmarks.json file to the JMH Visualizer.
The following command shows how to run a set of benchmarks very quickly, e.g. during development. The result is a very rough and imprecise overview of the expected execution time for the benchmarks:
java -jar ./benchmarks/target/benchmarks.jar '(quick)|(collections)' -f 1 -i 1 -wf 0 -bm ss -rff benchmarks.json -rf jsonThe parameters used are:
<regex>selects the benchmarks-f 1run a single fork per measurement-i 1use a single iteration for the fork-wf 0no warmup forks-bm ssMeasure "Single shot invocation time"-rff benchmarks.jsonwrite results to./benchmarks.json-rf jsonwrite result in JSON format
In JetBrains IntelliJ you can use these parameters as a run configuration while developing benchmarks.
For more information consult the command line help:
java -jar ./benchmarks/target/benchmarks.jar -hA. Y. Bhargava, Grokking Algorithms, Second edition. Shelter Island, NY: Manning Publications Co, 2024. [Online]. Available: https://livebook.manning.com/book/grokking-algorithms-second-edition/
A. Shipilëv and GitHub Contributors, “GitHub: openjdk/jmh.” Accessed: Mar. 02, 2025. [Online]. Available: https://github.com/openjdk/jmh
J. Jenkov, “JMH - Java Microbenchmark Harness.” Accessed: Mar. 02, 2025. [Online]. Available: http://jenkov.com/tutorials/java-performance/jmh.html
E. Baeldung, “Microbenchmarking with Java.” Accessed: Mar. 01, 2025. [Online]. Available: https://www.baeldung.com/java-microbenchmark-harness
A. Obregon, “Introduction to Java Microbenchmarking with JMH (Java Microbenchmark Harness),” Medium. Accessed: Mar. 02, 2025. [Online]. Available: https://medium.com/@AlexanderObregon/introduction-to-java-microbenchmarking-with-jmh-java-microbenchmark-harness-55af74b2fd38