binsearch benchmarks
2024-11-28
Binary search implementations and crude benchmarking
- V1: iterative straightforward solution
- V2: iterative slighly optimized solution
- one less comparison in the loop
bsearch()
: C stdlib'sbsearch
w/ customcompare()
/* Heap allocation due to large stack size needed given large N value
*
* $ gcc -O0 kr_0301.c -o kr_0301 && ./kr_0301 && rm kr_0301
*
* N: 100000000
* V1 seconds: 26.352719
* V2 seconds: 17.922611
* bsearch() : 9.016424
*
* $ gcc -O3 kr_0301.c -o kr_0301 && ./kr_0301 && rm kr_0301
*
* N: 100000000
* V1 seconds: 15.006659
* V2 seconds: 10.209212
* bsearch() : 4.310327
*
*/
I wasn't surprised regarding -O0
, but the fact V2 stays significantly faster even with highest performance optimizations is quite surprising. Grain of salt: Might be the case I'm not measuring what I think I'm measuring, benchmarking accurately is quite the science and the approach here not super sophisticated.
;
int64_t
int64_t
int
double
int
/* Heap allocation due to large stack size needed given large N value
*
* $ gcc -O0 kr_0301.c -o kr_0301 && ./kr_0301 && rm kr_0301
*
* N: 100000000
* V1 seconds: 26.352719
* V2 seconds: 17.922611
* bsearch() : 9.016424
*
* $ gcc -O3 kr_0301.c -o kr_0301 && ./kr_0301 && rm kr_0301
*
* N: 100000000
* V1 seconds: 15.006659
* V2 seconds: 10.209212
* bsearch() : 4.310327
*
*/