Thread: I can't figure out why this C code is running so slow...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    I can't figure out why this C code is running so slow...

    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...
    Last edited by Stachelsk; 01-27-2010 at 11:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't figure out what's with this code
    By silverstein101 in forum C++ Programming
    Replies: 8
    Last Post: 04-16-2008, 12:45 AM
  2. Replies: 27
    Last Post: 10-25-2007, 10:47 AM
  3. Running Exe in C++ Code.
    By Ti22 in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2006, 01:29 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM