简体   繁体   中英

Behavior of variables initialized inside inline function

I have a question about inline functions' initialized variables. I initialized 5 variables (using round() , ceil() , and fabs() ) inside a function but the problem was that the CPU takes about 1500 cycles to calculate their values.

Once I declared my function as inline functions it seems that the program and CPU no longer calculate these variables and also skip them in the debugger (despite working already fine as they were calculated before running the program or something). When checking the total cycles the 1500 cycles used for calculating them are gone now!

Question: What's happening in my case? Why CPU ignores calculating variables in inline functions and calculates them in normal functions?

Edit #1 : Here is what I mean: for inline function when I enter the function it goes directly to the highlighted line of code as it's the first line, here's the number of cycles required to go to the highlighted line of code:内联函数图像

and here is normal function, when I enter it it goes directly to initializing each variable line by line normally.正常功能图像

Thanks in advance

The fact that some library functions are inline is irrelevant, the compiler can evaluate any pure code at compile time and reduce it to its result as constants in the object code. How much of that is performed for a given piece of code depends on optimisation settings and performance with thresholds determined by the compiler implementors.

In your case, it seems the compiler can optimise the computation of these initializers when you declare the function as inline , probably because you call it with constant or even literal values. The intializers are probably computed at compile time and the resulting values are used in expressions as constants without even getting stored in the local variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM