Optimization Question

This is a discussion on Optimization Question within the C Programming forums, part of the General Programming Boards category; I have a program that I am optimizing. I desided to use a bit of in-line ASM to see if ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    7

    Question Optimization Question

    I have a program that I am optimizing. I desided to use a bit of in-line ASM to see if I'd notice any change in speed. I compiled the original version (before using ASM) and then the new version as a seperate file.

    Testing them back and forth doing the same exact tasks, I can't tell if it's faster, slower, or the same. I mean, it seems exactly the same speed, but I don't know. I guess since it's so close, it more than likely doesn't really matter how I choose to program it, but completely out of curiousity which would normally speed things up a bit (even if only by microseconds)? I have posted my code below.Which would be some-number tics faster?


    Code:
    /* Original */
    
    Bits[Input1] = (Bits[Input1] & 240) >> 4;
    // insert statements here...
    LB = 192 - LB - 224;
    BitFlag++;

    Code:
    /* This replaces the above entirely */
    
    Bits[Input1] = ProcessBits(Bits[Input1]);
    // insert statements here...
    LB = ProcessLB();
    
    static char ProcessBits(char Byte1)
    {
        __asm
        {
            mov     al, Byte1;
            and     al, 240;
            shr     al, 4;
        }
    }
    
    static char ProcessLB()
    {
        __asm
        {
            mov     al, 192;
            sub     al, LB;
            sub     al, 224;
            inc     BitFlag;
        }
    }
    Last edited by saxman; 06-27-2004 at 04:02 PM.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    see FAQ

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > Which would be some-number tics faster?
    The first one
    1. The compiler probably generates the same code as you've written in your asm statements
    2. The compiler doesn't call a function to do it.

    Example
    gcc -S prog.c
    Allows you to see the asm it produces. Find out how to do that for your compiler.

    > I have a program that I am optimizing.
    Well look at the big picture (the whole program), and use a profiler.
    Changing bubble sort into quicksort is an optimisation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    ... and on top of that your code probably will not even work properly bacause you forgot to "xor eax, eax" =)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Why would they need xor eax, eax ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    I guess you don't need it in when working with C, but it's like not calling CloseHandle() after CreateFile(), saying "Windows will close it for me". Which it does of corse, but it's kinda lame...
    Function return value is allways in eax, but he only initializes al, so higher 3 bytes are what have been passed by caller. Compilers usualy perform "movsx eax, al" or "and eax, 0xFF" operation, but you never know, do you?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    I'd expect use of al/ax/eax to be consistent with the return type, nothing more
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    Helps a bunch, thanks!


    Also, I don't see why EAX would have to be reset to zero either because no interrupts are being called within the ASM segment, and EAX returns the value I calculate within the segment anyway, so returning zero is definitely a no-no for what my program wanted with that portion of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 02:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 01:53 AM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21