1brc
1brc copied to clipboard
Improve spullara's solution (performs 25% better)
Copied spullara's solution and made a few improvements. Performs ~25% better on my machine.
- Class Name:
CalculateAverage_yemreinci - Machine specs: 16 cores, 64GB RAM
- JVM: 21.0.1-graalce
Improvements:
- Calculate the hashcode while reading the data, instead of later in the hashmap implementation. This is expected to increase instruction level parallelism as CPU can work on the math while waiting for data from memory/cache.
- Convert the number parsing while loop into a few if statements. While loop with a switch-case inside is likely not so great for the branch predictor.
Benchmarks
$ for i in {1..5}; do ./calculate_average_spullara.sh > /tmp/spullara.txt; done
real 0m4.096s
user 0m59.311s
sys 0m0.750s
real 0m4.117s
user 0m59.624s
sys 0m0.818s
real 0m4.160s
user 1m0.216s
sys 0m0.659s
real 0m4.119s
user 1m0.066s
sys 0m0.703s
real 0m4.059s
user 0m58.870s
sys 0m0.601s
$ for i in {1..5}; do ./calculate_average_yemreinci.sh > /tmp/yemreinci.txt; done
real 0m3.195s
user 0m45.039s
sys 0m0.840s
real 0m3.071s
user 0m43.438s
sys 0m0.731s
real 0m3.184s
user 0m44.765s
sys 0m0.718s
real 0m3.075s
user 0m43.489s
sys 0m0.759s
real 0m3.093s
user 0m43.659s
sys 0m0.692s
I added changes similar to yours. Very good!
Does this pass the tests: ./test.sh yemreinci?
00:13.463, nice. Thx for participating!