-1

NOTE: thankyou for telling me this will save next to nothing no matter how I write it, the reason I am asking is cause it is similar of my uni assignment and I would like to make my lecturer happy, is there a more 'readable' code or something better that would make my lecturer happier or is this code fine and not 'inefficient'.

I have a question on how i should write my code, basically I have a switch statement that is used to change a char to a specific char depending on what linenumber it is on. however it only changes if the linenumber is an odd number ( starting from 0, so every 2nd line itll pick a new char). currently my code is....

int linenumber;
char zone = UNKNOWN;

  for (linenumber = 0; linenumber < 21; linenumber++) {

     switch (linenumber) {
        case (1):
           zone = 'a';
           break;

        case (3):
           zone = 'b';
           break;

        case (5):
           zone = 'c';
           break;

        case (7):
           zone == 'd';
           break;

        case (9):
           zone = 'e';
           break;

        case (11):
           zone = 'f';
           break;

        case (13):
           zone = 'g';
           break;

        case (15):
           zone = 'h';
           break;

        case (17):
           zone = 'i';
           break;

        case (19):
           zone = 'j';
           break;

        if (linenumber % 2 == 0) {

        }
    }
}

however, since the switch only happens for every 2nd case, is it more efficient to write a if statement such as....

if (linenumber % 2 == 1) {
   switch.....
}

to put the switch statement inside... just wondering if an extra if statement would be more or less efficient in this situation.

Thanks in advance!

Rory Thoman
  • 47
  • 1
  • 6

3 Answers3

6

You are asking the wrong question. What you are trying to do is called micro-optimisation. It saves nanoseconds. Often it doesn't, because what you learnt three years ago might be wrong today. Real programmers don't care about nanoseconds, they go for big wins that at least save microseconds.

Seriously, write the code that is most readable. In this particular case, the switch statement is ridiculously unreadable.

For serious optimisation questions, look at Michael Kohne's answer. If you can't be bothered using a profiler, then the speed cannot have been important.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • thanks for this, as you can tell I am new and I'm asking this question not necessarily to make it run the fastest possible way, but because it is related to an assignment I'm doing and would like to keep the lecturer happier. you stated that the switch statement is unreadable, can you elaborate and suggest a better way? – Rory Thoman Dec 22 '14 at 12:09
  • RE: "can you elaborate and suggest a better way?" As other pointed out, you are not only micro-optimizing but the code is not particularly readable. Often, a more readable solution is also faster: ` int zone = UNKNOWN; if (0 <= linenumber && linenumber <= 19){ zone = linenumber[".a.b.c.d.e.f.g.h.i.j"]; } if (zone == '.'){ zone = UNKNOWN; } ` (sorry about bad formatting, due to mini-markdown. needs line breaks and whitespace) – frasnian Dec 23 '14 at 13:48
2

There's no answer except 'profile it both ways and see'. It depends on the compiler and the target environment.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
2

These sorts of micro-optimizations are unlikely to affect the performance of your code.

Also, if you NEEDED to care, then you should be doing performance profiling on your code. In which case finding out the performance difference between a switch case and an if-else block would be trivial.

Edi G.
  • 2,432
  • 7
  • 24
  • 33