Time critical

This is a discussion on Time critical within the C Programming forums, part of the General Programming Boards category; I need this done in the next 15 seconds! j/k, of course. I wrote a program in a different language ...

  1. #1
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

    Time critical

    I need this done in the next 15 seconds!

    j/k, of course.

    I wrote a program in a different language (ObjectPAL) a couple years ago, and execution speed was very (X100) important to my boss, so much so that he made us replace every if statement with a switch statement with only one case, because he said it was faster. So - would that be language specific, or is it a difference that could be attributed to the difference in data structures? Which one's faster in C?

  2. #2
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I can't see how that would be faster. A good compiler should generate the same machine code for both the if and switch/case strategy.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >because he said it was faster
    I don't know of any speed difference between and if and a switch with one case. The if will take a value and test it against another value and if the result is true then the body is executed. The switch takes a value and tests it against a series of values, but in this case it only tests against one and if it is true the body is executed. Provided that no break statement is included in the one case switch they should perform equally.

    I would imagine that a switch with multiple cases would perform more efficiently than an if..else construct with the same tests because the switch only acquires the original value once, just like foo += 1, foo is only accessed once instead of twice in the longer version foo = foo + 1;.

    >execution speed was very (X100) important
    Fiddling around with the comparisons and smaller stuff should be done after the most efficient algorithm has been designed. You won't see as much of a speed increase by changing ifs to switches than you would if the program as a whole were redesigned to be efficient. Though you may have already done this at the time, most bosses concerned with efficiency and speed won't look at the big picture.

    >would that be language specific
    Probably

    -Prelude
    My best code is written with the delete key.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > so much so that he made us replace every if statement with a switch statement

    And was it quicker?

    And was it just technically quicker (like you need an oscilloscope), or was is really quicker from a user perspective?

    Did anyone actually bother to measure the before and after case?

    Think about what you actually did - the hugely significant part of an if statement is the expression, not the jump if true/false, which even the lamest of compilers would have trouble generating more than a handful of instructions for.

    In C, they would have to be of the form
    if ( var == CONSTANT )
    for that hack to work, and it's only at this level of simplicity that the time spent evaluating the condition, and the subsequent jumps become comparable.

    As Prelude said, its pointless micro-optimising until you've chosen the best algorithm.

    It's also pointless taking a blanket approach, without having profiled the code to find out where it really is spending the most time. Saving 1uS in some initialisation code which is run just once is not value for money.

    > would that be language specific
    - The language, the compiler, the version of the compiler, the compilation options.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > And was it quicker?

    Not noticeably, no... However, I wasn't exactly in a position to argue with him.

    Don't blame me for this - it was his idea, and I never said it was a good one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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