This question already has answers here:
Is x += 1 more efficient than x = x + 1?
(6 answers)
Incrementing: x++ vs x += 1
(5 answers)
Why is i=i+1 faster than i++?
(5 answers)
Closed 8 years ago.
Is there is any difference between these two statements performance wise ?
i++;
i = i + 1;
Depends on the optimisation. i++ can, on most processors, be represented as a single machine language instruction. i = i + 1, on the other hand, could be represented by up to four: load i, load 1, add, store to i; although, even a middling smart compiler should be able to recognise it can rewrite it into the former.
no, there is no difference. The compiler will compile these into the same assembly
Related
This question already exists:
Analysis of the operation order of logical operators [closed]
Closed 11 days ago.
What is the output result of the following formula
We have known
int a,b,c;
a = 3;
b = 4;
c = 5;
a||+c&&b-c;
After running the code, I get the answer as 1.
I know the order of operators, but it is difficult to use them.
But how to analyze the result, that is, what is the detailed operation?
I want to know how this+participates in the operation.
This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 4 years ago.
I am not able to understand this code
#define sqt(x) x*x
int main(){
print("%d",sqt(3+1));
}
Manually I am getting the output of 10. But when write the code and compile it I am getting the answer as 7. Please explain this.
Remember, since you're using a macro, 3 + 1 is not evaluated before sqt is called. x becomes 3 + 1 (not 4), then order of operation causes an unexpected answer to be produced since addition happens after multiplication.
Or in other words:
sqt(3 + 1)
expands to:
3 + 1 * 3 + 1
Then, when you evaluate this like you would any other equation:
3 + 1 * 3 + 1 // Multiplication happens first
= 3 + 3 + 1
= 7
This is a good example of why you shouldn't use macros unless they're strictly necessary, or you've made proper care to ensure that things like order of operation mistakes don't happen. As #Barmar points out this particular case can be remedied by having the macro expand to include explicit parenthesis:
#define sqt(x) ((x)*(x))
Which would cause the evaluation to differ and give a proper answer:
(3 + 1) * (3 + 1)
4 * 4
16
This question already has answers here:
Rearranging an equation
(7 answers)
Closed 8 years ago.
I am working on C code optimization and there are many calculations which adds 1.0 to the expression which returns double value like
val = 1.0 + u[8] / c_sq + (u[8] * u[8]) / (2.0 * c_sq * c_sq) - u_sq / (2.0 * c_sq)
so I was just curious to know is there any optimization technique to improve this piece of code.
That single line of code, taken on its own without any context whatsoever, is what it is. It cannot be optimized any further as long as the compiler you are using is at least doing CSE on the 2.0 * c_sq. Otherwise, that's pretty much all you can do outside of domain-specific optimizations that aren't apparent by just that code.
On typical current processors, division is time consuming; it can take dozens of CPU cycles. You can rearrange the expression to eliminate two divisions, per this answer. You can make some minor other improvements as well (which the compiler might have caught):
double t = u[8];
double v = 2*c_sq;
val = 1 + (t*(v+t) - u_sq*c_sq) / (v*c_sq);
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 10 years ago.
Cannot explain the output of the following program. According to my knowledge the output should be 19, but running it gives me output of 20. I used gcc to compile this program.
int main()
{
int x, y = 5;
x = ++y + ++y + --y;
printf("%d", x);
return 0;
}
Your program exploits undefined behavior as you modify y multiple times between two sequence points (in your case, the end of the statement). If you turn on warnings with -Wall, your compiler is probably even going to warn you about that.
6+7+6 = 19
so 19 will be your output
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
How can X[i] possibly be interpretted the same as i[X] in C?
I think the question is asking how an array[elementInArray] can be interpretted the same as elementInArray[array]
Also, unrelated on the same homework:
"Explain why the "hidden bit" of floating point format does not need to be represented." (in terms of 32 bit words of binary)
In C, X[i] = i[X] = *(X + i) = *(i + X). As per commutative property, they are same.