Simple C/C++ doubts

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!
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.
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

1
2
3
4
5
6
7
8
9
10
11
#define MACRO_SIZE 10
int main( void )
{
  const int SIZE = 10;
  int a[SIZE]
  int b[SIZE+50];
  int c[SIZE*50];
  int d[10/3];
  int d[MACRO_SIZE];
  int e[MACRO_SIZE+SIZE*2];
}


Try to compile this in bothC and C++.
Last edited on
closed account (o1vk4iN6)
Chervil wrote:
Actually using a global variable or a local variable, there is no difference in speed.


Which is faster ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

class A
{
public:
     virtual void foo() = 0;
};

class B : public A
{
public:
     virtual void foo(){ }
};

B gb;

int main()
{
     B b;

     gb.foo();
     b.foo();
}
Last edited on
kg1992


What do you want to conclude from the second example?

xeri
:
I donno which is faster? :D and why?
I use Visual Studio 2010!
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.
xerzi wrote:
Which is faster ?

As written, compiling that program results in an empty main() function:
1
2
3
main:
        xorl    %eax, %eax
        ret

When I make B::foo() actually do something, I get two exactly identical inlined B::foo() function bodies:

given
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
volatile int n;
class A
{
public:
     virtual void foo() = 0;
};

class B : public A
{
public:
     virtual void foo(){ n = 1; }
};

B gb;

int main()
{
     B b;

     gb.foo();
     b.foo();
}

the compiled (with GCC) program is:

1
2
3
4
5
main:
        movl    $1, n(%rip)
        xorl    %eax, %eax
        movl    $1, n(%rip)
        ret


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
closed account (o1vk4iN6)
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.
Thanks all..But no one answered my first question correctly :(
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
Good question!
I tried to answer the first question using the following source code:

1
2
3
4
5
6
7
8
9
10
11
12
static int a;
int fTest();
int main() {
  int b;
  a=a+11;
  b=b+22;
}
int fTest(){
  int c;
  c=c+33;
}


gcc with option -S give us the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	.file	"variabili.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	andl	$-16, %esp
	movl	$0, %eax
	addl	$15, %eax
	addl	$15, %eax
	shrl	$4, %eax
	sall	$4, %eax
	movl	%eax, -8(%ebp)
	movl	-8(%ebp), %eax
	call	__alloca
	call	___main
	addl	$11, _a
	leal	-4(%ebp), %eax
	addl	$22, (%eax)
	leave
	ret
.globl _fTest
	.def	_fTest;	.scl	2;	.type	32;	.endef
_fTest:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$4, %esp
	leal	-4(%ebp), %eax
	addl	$33, (%eax)
	leave
	ret
.lcomm _a,16


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:


1
2
3
int main() {
int a=1+3;
}


And, again, gcc with -S option give us:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	.file	"ottimo.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	andl	$-16, %esp
	movl	$0, %eax
	addl	$15, %eax
	addl	$15, %eax
	shrl	$4, %eax
	sall	$4, %eax
	movl	%eax, -8(%ebp)
	movl	-8(%ebp), %eax
	call	__alloca
	call	___main
	movl	$4, -4(%ebp)
	leave
	ret


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. :)
@infoartenovo: discussing performance of a C++ program with optimization disabled is pointless.
@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
Topic archived. No new replies allowed.