Consequences of Global vs. Local Variable in Arduino Code

gvl

This is an overly simplistic comparison of the consequences of using a global vs. local variable on the Arduino. Beyond the obvious variable scope, take note of the program size trade off (code and data segments), startup code differences, stack impact, and potential performance consequences.

Global Variable Program:

long volatile x;

void setup() { }

void loop() {
  x = random(10);
}

Local Variable Program:

void setup() { }

void loop() {
  long volatile x;
  x = random(10);
}

Compile time report from the IDE:
Global sketch uses 960 bytes.
Global variables use 17 bytes of dynamic memory.

Local sketch uses 976 bytes.
Local variables use 13 bytes of dynamic memory.

It’s not the overall program size, but rather the comparison we are concerned with. The local version uses 16 more flash bytes, but 4 less SRAM bytes.

Disassembly of relevant portions of the programs:
Note the differences in the startup code along with the code inside loop().

Global:

. . .
do_clear_bss_start:
  7e:	a1 31       	cpi	r26, 0x11	; 17
  80:	b2 07       	cpc	r27, r18
  82:	e1 f7       	brne	.-8      	; 0x7c <.do_clear_bss_loop>
__do_copy_data:
  84:	11 e0       	ldi	r17, 0x01	; 1
  86:	a0 e0       	ldi	r26, 0x00	; 0
  88:	b1 e0       	ldi	r27, 0x01	; 1
  8a:	ec eb       	ldi	r30, 0xBC	; 188
  8c:	f3 e0       	ldi	r31, 0x03	; 3
  8e:	02 c0       	rjmp	.+4      	; 0x94 <__do_copy_data+0x10>
  90:	05 90       	lpm	r0, Z+
  92:	0d 92       	st	X+, r0
  94:	a4 30       	cpi	r26, 0x04	; 4
  96:	b1 07       	cpc	r27, r17
  98:	d9 f7       	brne	.-10     	; 0x90 <__do_copy_data+0xc>
  9a:	0e 94 88 00 	call	0x110	; 0x110 <main>
  9e:	0c 94 dc 01 	jmp	0x3b8	; 0x3b8 <_exit>
. . .
loop:
  a8:	6a e0       	ldi	r22, 0x0A	; 10
  aa:	70 e0       	ldi	r23, 0x00	; 0
  ac:	80 e0       	ldi	r24, 0x00	; 0
  ae:	90 e0       	ldi	r25, 0x00	; 0
  b0:	0e 94 63 00 	call	0xc6	; 0xc6 <_Z6randoml>
  b4:	60 93 04 01 	sts	0x0104, r22
  b8:	70 93 05 01 	sts	0x0105, r23
  bc:	80 93 06 01 	sts	0x0106, r24
  c0:	90 93 07 01 	sts	0x0107, r25
  c4:	08 95       	ret
. . .

Local:

. . .
do_clear_bss_start:
  7e:	ad 30       	cpi	r26, 0x0D	; 13
  80:	b2 07       	cpc	r27, r18
  82:	e1 f7       	brne	.-8      	; 0x7c <.do_clear_bss_loop>
__do_copy_data:
  84:	11 e0       	ldi	r17, 0x01	; 1
  86:	a0 e0       	ldi	r26, 0x00	; 0
  88:	b1 e0       	ldi	r27, 0x01	; 1
  8a:	ec ec       	ldi	r30, 0xCC	; 204
  8c:	f3 e0       	ldi	r31, 0x03	; 3
  8e:	02 c0       	rjmp	.+4      	; 0x94 <__do_copy_data+0x10>
  90:	05 90       	lpm	r0, Z+
  92:	0d 92       	st	X+, r0
  94:	a4 30       	cpi	r26, 0x04	; 4
  96:	b1 07       	cpc	r27, r17
  98:	d9 f7       	brne	.-10     	; 0x90 <__do_copy_data+0xc>
  9a:	0e 94 90 00 	call	0x120	; 0x120 <main>
  9e:	0c 94 e4 01 	jmp	0x3c8	; 0x3c8 <_exit>
. . .
loop:
  a8:	cf 93       	push	r28
  aa:	df 93       	push	r29
  ac:	00 d0       	rcall	.+0      	; 0xae <loop+0x6>
  ae:	00 d0       	rcall	.+0      	; 0xb0 <loop+0x8>
  b0:	cd b7       	in	r28, 0x3d	; 61
  b2:	de b7       	in	r29, 0x3e	; 62
  b4:	6a e0       	ldi	r22, 0x0A	; 10
  b6:	70 e0       	ldi	r23, 0x00	; 0
  b8:	80 e0       	ldi	r24, 0x00	; 0
  ba:	90 e0       	ldi	r25, 0x00	; 0
  bc:	0e 94 6b 00 	call	0xd6	; 0xd6 <_Z6randoml>
  c0:	69 83       	std	Y+1, r22	; 0x01
  c2:	7a 83       	std	Y+2, r23	; 0x02
  c4:	8b 83       	std	Y+3, r24	; 0x03
  c6:	9c 83       	std	Y+4, r25	; 0x04
  c8:	0f 90       	pop	r0
  ca:	0f 90       	pop	r0
  cc:	0f 90       	pop	r0
  ce:	0f 90       	pop	r0
  d0:	df 91       	pop	r29
  d2:	cf 91       	pop	r28
  d4:	08 95       	ret
. . .
Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s