| SameerThigale (90) | |
|
1. Which variables are the most fast to perform actions? Global variables or local variable? Why? Are static variables fast than auto? Why? Are dynamic variables more fast to execute than static one's? Why? Which one's are stored on stack? and which one's on heap? * I know, register variables are fastest though!! 2. int a=1+3;is 1+3 computed by the compiler itself before computing the code? Or is 1+3 computed at run-time and then the value is assigned?Thanks a lot for your help! I'd a lot of confusions! | |
|
|
|
| Chervil (812) | |
|
Actually using a global variable or a local variable, there is no difference in speed. The difference is only in creating and destroying the variable at the start and end of the block of code. Register variables may not be faster. The CPU has a limited number of registers, and it needs to use some of these to hold addresses, carry out intermediate calculations and so on. Thus not all requests for the use of a register can be honoured. An expression such as 1+3 evaluates to a constant. You'd certainly expect the compiler to carry out this. | |
|
|
|
| kg1992 (30) | |||
About Question number 2, one way to check if such thing is compile-time constant or not is actually giving such thing to where compiler needs them, and see if it can be compiled or not.int a[1+5];This certainly compiles anyway. The next example is more interesting
Try to compile this in bothC and C++. | |||
|
Last edited on
|
|||
| xerzi (570) | |||||
Which is faster ?
| |||||
|
Last edited on
|
|||||
| SameerThigale (90) | |||
What do you want to conclude from the second example?
I donno which is faster? :D and why? | |||
|
|
|||
| SameerThigale (90) | |
| I use Visual Studio 2010! | |
|
|
|
| kg1992 (30) | |
| I just want to show you something that might be fun to try. No special intention except that C and C++ compilers treats const variable differently. Try out and see what happens. It will be fun. | |
|
|
|
| Cubbi (1583) | |||||||||
As written, compiling that program results in an empty main() function:
When I make B::foo() actually do something, I get two exactly identical inlined B::foo() function bodies: given
the compiled (with GCC) program is:
It is not easy to make a test that would show any performance difference between accessing objects with automatic and static storage, and the difference may be in either direction depending on the situation. | |||||||||
|
Last edited on
|
|||||||||
| xerzi (570) | |
| Ah I was actually thinking of two different scenarios but then in either case they would perform the same my bad. In case of B being a pointer in a parameter it would be virtual, not inlined. | |
|
|
|
| SameerThigale (90) | |
| Thanks all..But no one answered my first question correctly :( | |
|
|
|
| BlackSheep (388) | |
|
1. Which variables are the most fast to perform actions? Global variables or local variable? Why? //Same speed, one lookup Are static variables fast than auto? Why? //I'm assuming you mean the old //"C" version of auto. Then, no difference. Even if you meant the new C++11 //auto, there's still no difference, since it's evaluated at compile time. //(Assuming the variable is stack based.) Are dynamic variables more fast to execute than static one's? Why? //No, dynamic variables are on the heap which has slower access, //and more problematically suffers from major allocation/deallocation overhead. //The access time comes from the fact that you first access the value //stored at the pointer's location, and then use that to index //the value you actually want. Which one's are stored on stack? and which one's on heap? //Anything declared with new goes on the heap, everything else on the stack. (Extreme oversimplification) Note that this is a huge generalization. Your CPU will stick some data into its cache, might even save some often used variables in the registers, and your compiler will pull tricks on you that will seem like they make absolutely no sense. | |
|
Last edited on
|
|
| infoartenovo (15) | |||||||||
|
Good question! I tried to answer the first question using the following source code:
gcc with option -S give us the following:
Note that I used the int constant 11, 22 and 33 to identify easily the fragment of assembler code of interest. We can observe that: 1) For what concern the global variable b and the local variable c, they are both popped from the stack and then the addition is performed; 2) For what concern the static int variable a the compiler use the addl instruction with the absolute address of the space reserved to the variable a with the .lcomm _a,16 assembly directive. My friends it seems that static variable perform better than global and local variables. For what concern the second question, I wrote the following code:
And, again, gcc with -S option give us:
As we can see, the compiler, even without teh selection of optimization options, optimize the code and translate a= 1+3 as movl $4, -4(%ebp), that is a =4. Q.E.D. :) | |||||||||
|
|
|||||||||
| Cubbi (1583) | |
| @infoartenovo: discussing performance of a C++ program with optimization disabled is pointless. | |
|
|
|
| infoartenovo (15) | |
| @Cubbi: Probably, my test description wasn't clear. Anyway, I just left standard option. In other words, I compiled the code with gcc -S filename.c. Ciao | |
|
Last edited on
|
|