Created
August 11, 2010 15:17
-
-
Save haxney/519139 to your computer and use it in GitHub Desktop.
The speed differences between registers and memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main () { | |
int loop; | |
for (loop=0; loop<500000000; loop++); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main (int argc, char *argv[]) { | |
int i = 47; | |
int loop; | |
for (loop=0; loop<500000000; loop++) | |
i + i; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main () { | |
register int loop; | |
for (loop=0; loop<500000000; loop++); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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