-2

Consider the Following Function taking into account

  1. addition costs you 4 Operations
  2. assignment costs 1 Operations
  3. Comparision costs 1 Operations

Calculation of the above function will cost 14 Operations

int function1(int a,int b,int c, int d, int e)
{
int returnNumber;
//int no = randomNumber(); //Some Random Number Generator Function , Lets Assume the cost of this function to be 0 for simplicity purpose

switch(randomNumber())
   {
   case 0: returnNumber = a+b;  // costs 6 Operations , Case Check costs 1, assignment costs 1 and addition costs 4
           break;
   case 1: returnNumber = c+d;  // Costs 6 Operations 
           break;
   default: returnNumber = e;   // costs 2 Operations
   }
 return returnNumber;
}

Total Cost of this Function is 14 Operations , Is there any code which can do the same thing as well as reduce the cost of this function atleast by 1 ?

EDIT 1 Added the Break Statement in Switch Statements as well as assignment of variable no to a randomNumber generator function

EDIT 2 i came across this question from a friend of mine who happened to have faced this question in the F2Fs , i wonder if this question has a solution or not , since i havent been able to find any till Now.

del_champ
  • 179
  • 1
  • 9

3 Answers3

8

As originally written, the switch block had no break statements, so the returned value would always be the result of the last case, regardless of the random number generated. The whole function therefore could have been simplified to:

int function1(int a,int b,int c, int d, int e)
{
   return e;
}

The current version could be adjusted to:

int function1(int a,int b,int c, int d, int e)
{
    switch(randomNumber())
    {
       case 0: return a+b;
       case 1: return c+d;
       default: return e;
    }
}

which by your metrics would result in one fewer variable assignment per case. (However, any decent compiler would have optimized it away anyway.)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • This is nitpicking, but pseudo random number generators typically have side-effects, so you should have call to `randomNumber();` to be truly equal with original function. – user694733 Nov 16 '16 at 08:20
  • @user694733 Good point. However, I am going to assume that `randomNumber()` is an ideal random (and not pseudo-random) number generator. ;) – jamesdlin Nov 16 '16 at 08:23
  • @jamesdlin the OP has edited his question, there are `break` statements now, so this answer is no longer an answer. – Jabberwocky Nov 16 '16 at 08:49
0

You can reduce 1 Operation by not asigning no, and use the return value of the randomNumber() directly in you switch, also you forgot the break after the switch's cases unless it was intentional.

    switch(randomNumber())
   {
   case 0: returnNumber = a+b;break;
   case 1: returnNumber = c+d;break;
   default: returnNumber = e;
   }
  • @KapilBhandari: you are not supposed to edit the original question with corrections from the answers and comments, it makes the whole discussion inconsistent. Btw, for consistency, unless you make all cases perform a direct `return`, you should have a `break;` statement in the `default` cause as well: this would make the code a tad more resistant to further edits and quicker to analyze visually. – chqrlie Nov 16 '16 at 09:07
  • @chqrlie : Will keep in mind for future Questions Thanks – del_champ Nov 16 '16 at 09:10
0

The cost of these operations cannot be assessed without a look at the actual code generated. And the code generation depends on many factors such as the choice of target, compiler and compilation options. Optimizing compilers can reorganize the code to improve efficiency or reduce code size, micro-optimizing at the statement level such as what is requested here is a moot point.

Furthermore, the cost of the function call and that of the switch dispatch are likely to dwarf the minuscule timings of the single operations.

Storing the result in a local variable or returning it directly should make no difference for modern optimizing compilers, it is mostly a matter of coding conventions.

If local coding conventions specify that there should be a single return statement, use this:

// the prototype for randomNumber should come from a header file
int randomNumber(void); //Some Random Number Generator Function

int function1(int a, int b, int c, int d, int e) {
    int returnNumber;

    switch (randomNumber()) {
      case 0:
        returnNumber = a + b;
        break;
      case 1:
        returnNumber = c + d;
        break;
      default:
        returnNumber = e;
        break;
    }
    return returnNumber;
}

If you can have multiple return statements, use the simpler version:

int function1(int a, int b, int c, int d, int e) {
    switch (randomNumber()) {
      case 0:
        return a + b;
      case 1:
        return c + d;
      default:
        return e;
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189