Kinghajj's Blog

2009-10-24

Clang vs. GCC Benchmark with Heap Sort

Last year I wrote an implementation of heap sort in C. I then wrote some code to benchmark the sorting times between it and the standard C qsort(), and the results were that my hsort() was only about 1.5x slower than qsort(); not too shabby!

You can get the code from Pastebin:
  1. hsort.c
  2. hsort.h
  3. main.c
Last night I decided to install Clang so I could see if it could produce better machine code than GCC with my heapsort code, so I installed clang-svn 84993 from the Arch Linux AUR, and compiled the code with -O3 with both GCC and Clang and saved the output from each program to seperate text files. The results for Clang and GCC are again on pastebin.

What's interesting about these results is that, while the average hsort times went down from 54 ms in GCC to 51 ms in Clang, the average qsort times went up from 34 ms in GCC to 35 ms in Clang. I didn't expect this at all, but I suspect it's some sort of fluke. Maybe someone with more experience with Clang and/or GCC could shed some light on this.