Do temp variables slow down my program?

前端 未结 5 864
悲&欢浪女
悲&欢浪女 2020-11-28 08:42

Suppose I have the following C code:

int i = 5;
int j = 10;
int result = i + j;

If I\'m looping over this many times, would it be faster to

5条回答
  •  孤街浪徒
    2020-11-28 08:52

    While all sorts of trivial differences to the code can perturb the compiler's behavior in ways that mildly improve or worsen performance, in principle it it should not make any performance difference whether you use temp variables like this as long as the meaning of the program is not changed. A good compiler should generate the same, or comparable, code either way, unless you're intentionally building with optimization off in order to get machine code that's as close as possible to the source (e.g. for debugging purposes).

提交回复
热议问题