Hi,
I recently learned a fair amount of inline assembly and decided to do a little test. I wrote a selection sort in C that sorts 100,000 integers. I then wrote a inline assembly version of the same thing. I figured that the assembly version was going to be a little slower (I was pretty sloppy and didn't worry about optimization too much), but when I ran the tests I was shocked to find out that the assembly version was running twice as fast!
The C code is
Code:
for (i = 0; i < n-1; ++i) {
min = i;
for (j = i+1; j < n; ++j)
if (data[j] < data[min])
min = j;
if (i != min) {
j = data[i];
data[i] = data[min];
data[min] = j;
}
}
I wrote the assembly code by looking at the C code and working my way through it. It's not heavily optimized, but it's not written extremely poorly either. My compiler is gcc 4.2.1, and I'm compiling both the C and inline assembly versions with -Wall -arch i386 -O2.
time ./test_asm < randfile.txt > outfile.txt
real 0m6.431s
user 0m6.381s
sys 0m0.012s
time ./test_c < randfile.txt > outfile.txt
real 0m13.518s
user 0m13.477s
sys 0m0.016s
Is assembly really THAT much faster??
EDIT: I am not attempting to start a flame war...