Skip to content

Instantly share code, notes, and snippets.

@haxney
Created August 11, 2010 15:17
Show Gist options
  • Save haxney/519139 to your computer and use it in GitHub Desktop.
Save haxney/519139 to your computer and use it in GitHub Desktop.
The speed differences between registers and memory
--- memory.s 2010-08-11 11:14:35.503392530 -0400
+++ register.s 2010-08-11 11:14:29.988393279 -0400
@@ -1,19 +1,20 @@
- .file "memory.c"
+ .file "register.c"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
- subl $16, %esp
- movl $0, -4(%ebp)
+ pushl %ebx
+ movl $0, %ebx
jmp .L2
.L3:
- addl $1, -4(%ebp)
+ addl $1, %ebx
.L2:
- cmpl $499999999, -4(%ebp)
+ cmpl $499999999, %ebx
jle .L3
- leave
+ popl %ebx
+ popl %ebp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
$ gcc memory.c && time ./a.out
real 0m2.183s
user 0m2.092s
sys 0m0.004s
$ gcc register.c && time ./a.out
real 0m0.315s
user 0m0.300s
sys 0m0.004s
void main () {
int loop;
for (loop=0; loop<500000000; loop++);
}
.file "memory.c"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $0, -4(%ebp)
jmp .L2
.L3:
addl $1, -4(%ebp)
.L2:
cmpl $499999999, -4(%ebp)
jle .L3
leave
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
.section .note.GNU-stack,"",@progbits
#include <stdio.h>
int main (int argc, char *argv[]) {
int i = 47;
int loop;
for (loop=0; loop<500000000; loop++)
i + i;
return 0;
}
void main () {
register int loop;
for (loop=0; loop<500000000; loop++);
}
.file "register.c"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl $0, %ebx
jmp .L2
.L3:
addl $1, %ebx
.L2:
cmpl $499999999, %ebx
jle .L3
popl %ebx
popl %ebp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
.section .note.GNU-stack,"",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment