Which is faster?

This is a discussion on Which is faster? within the C++ Programming forums, part of the General Programming Boards category; Which snippet is faster? I'm pretty sure B is, but I'm not sure if the extra var dec is hurting. ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007

    Which is faster?

    Which snippet is faster? I'm pretty sure B is, but I'm not sure if the extra var dec is hurting.
    // Snippet A
    for(x = 0; x < 1000; x++)
       buff1[x * 10] = x;
       buff2[x * 10] = x;
    // Snippet B
    for(x = 0; x < 1000; x++)
       DWORD nx = x * 10;
       buff1[nx] = x;
       buff2[nx] = x;
    “Is that when I became booksexual?” — phantomotap
    “I just want to use my computer in a productive manner, not learn how to use it.” — Elysia
    “A year spent in artificial intelligence is enough to make one believe in God.” — Alan J. Perlis

  2. #2
    Join Date
    Dec 2007
    I would think any modern compiler would optimize both to be the same.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    The second should be faster, but why not just compile your code and see for yourself? Some things to note also are that the optimizer may just generate the most optimal way of doing that either way. Also, one last thing: standard logic applies. Multiplication can be expensive, but even if it weren't, which is faster, doing something once or twice?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    CSE should make them the same with any decent compiler.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    And CSE is part of the optimizing functions even in Turbo C and other quite old and limited compilers, so if you haven't got it in your current compiler, you probably should look for a better one.

    However, sometimes it's a good idea to add an extra variable simply because it makes it easier to see that the calculation of two expression are the same in different places.

    [And by the way, I'd expect the compiler do something like this out of that code:
    for(x = 0, nx = 0; x < 1000; x++, nx += 10)
       buf1[nx] = x;
       buf2[nx] = x;
    , as multiplying by 10 is much more work than adding two variables.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 02:56 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Computations - which is faster?
    By ulillillia in forum C Programming
    Replies: 9
    Last Post: 12-09-2006, 10:23 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 10:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

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