1

As I understand, if I have a:

if(case 1)
{
  char x[] = "1";
  printf("%s",x);
}
else if(case 2)
{
  char x[] = "12";
  printf("blah-blah-blah\n");
  printf("%s",x);
}

then my compiler will try to predict the branch the code will enter, and this way, to improve the execution flow. I can create my char x[MAX_SIZE] in the head of this function and this way avoid a redundant declaration (if the branch predictor is wrong) but that would make me create a longer array than I would probably need...

Asking performance-wise: is it a good idea to move the array declaration to my function's head, or leave it inside each case of my if-else?

EDIT: I know that any performance change in this code will be very little and probably won't be recognized, but the question was about the principal.

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • Note: `"1"` is not a `char` - it is a `string`. As for performance, your optimization and memory saving is negligible. – abhishek_naik Jun 26 '16 at 10:47
  • 3
    In this example, the influence of the branch prediction is futile. The runtime is completely dominated by the printf() function calls. – wildplasser Jun 26 '16 at 10:52
  • @BatCoder I meant "1" to be a string. Did I mention otherwise? – CIsForCookies Jun 26 '16 at 11:29
  • The concrete effect of branch prediction (any many other workings of your CPU) will depend on context. Any optimization done now is useless with great probability once you change the code. The code very much looks unfinished. – Peter G. Jun 26 '16 at 11:41
  • @PeterG. this code is just an example - what I wanted to know is whether the array declaration should be inside the branch or outside, with no other compiler opt. – CIsForCookies Jun 26 '16 at 11:45

2 Answers2

3

In your case declarations of char x[] are not redundant, even though the objects they declare have the same name. However, since these objects have different scope, everything else is different about them, including their type (one is char[2], the other is char[3]).

It does not look like you need your x objects to be writeable, so you could use a pointer, like this:

char *x;
if (case 1) {
    x = "1";
} else if (case 2) {
    x = "12";
    printf("blah-blah-blah\n");
}
printf("%s", x);

This approach avoids copying string literals into writeable memory, potentially saving you a lot more than a good branch prediction.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
-1

As far as I could understand your question , Although, it depends on the requirement and may not be applicable always but you should probably go with char[Max_Size] as per conventions and in accord with good programming practice of writing readble code

Also it is a good practice to avoid redundancy.

Moreover ,as far as you are concerned about wasting resources...

it would probably be more wasted in a situation like

fun1(){
char x[10000];
...
}

fun2(){
char x[5000];
...
}

fun3(){
char x[10000];
...
}

rather than a simple char x[MAX_SIZE];

hope it helps :)

eRaisedToX
  • 3,221
  • 2
  • 22
  • 28