slow function

This is a discussion on slow function within the C Programming forums, part of the General Programming Boards category; Code: int dx, dy; ... return ((dx << 4) + (dx << 2) + (dy << 4) - (dy << ...

  1. #1
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59

    slow function

    Code:
    int dx, dy;
    ...
    return ((dx << 4) + (dx << 2) + (dy << 4) - (dy << 2)) >> 4;
    Seems much too slow for what it is. (using GCC 3.2 -O3)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    How did you measure how long it takes?
    It generates all of 11 instructions here, difficult to see how it could be any less since you start out with 8 operators in your expression.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Could simplify to:
    (5 * dx + 3 * dy) / 4
    though since you like using the shifts:
    (dx + (dx << 2) + dy + (dy << 1)) >> 2
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Ah, it's not premature optimisation at work is it?
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Ah, it's not premature optimisation at work is it?
    Yeah the bottleneck probably lies elsewhere, it's not like the formula is particularly complex.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    It's part of a slowish rendering thing, and runs this function hundreds of times per pixel. There are a couple of oddities in the generated code, like jumping to a subroutine just to negate ecx, then jumping back. I'll try rewriting it in asm, thanks for the help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Get rid of your function. Make it a macro.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by quzah
    Get rid of your function. Make it a macro.


    Quzah.

    Exactly, it's probably the overhead of calling the function more than anything.

    Alternately you could put "inline" before the function definition but making it a macro would be better practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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