Created
July 17, 2012 20:50
-
-
Save sehe/3131989 to your computer and use it in GitHub Desktop.
Intel Inline Assembly loop benchmark
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> | |
#include<stdlib.h> | |
#include<time.h> | |
int main() | |
{ | |
int j=0; | |
int a=0,b=0,temp=0; | |
srand(time(0)); | |
time_t t1=0; | |
time_t t2=0; | |
t1=clock(); | |
for(int i=0; i<200000000; i++) | |
{ | |
temp=a;//instruction 1 | |
a=b;//instruction 2 | |
b=temp;//3 instructions total | |
} | |
t2=clock(); | |
printf("\n time of for-loop(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
while(j<200000000) | |
{ | |
temp=a;//again it is three instructions | |
a=b; | |
b=temp; | |
j++; | |
} | |
t2=clock(); | |
printf("\n time of while-loop(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000;//setting the count | |
__asm | |
{ | |
pushf //backup | |
push eax //backup | |
push ebx //backup | |
push ecx //backup | |
push edx //backup | |
mov ecx,0 //init of loop range(0 to 200000000) | |
mov edx,j | |
do_it_again: //begin to loop | |
mov eax,a //basic swap steps between cpu and mem(cache) | |
mov ebx,b | |
mov b,eax | |
mov a,ebx //four instructions total | |
inc ecx // j++ | |
cmp ecx,edx //i<200000000 ? | |
jb do_it_again // end of loop block | |
pop edx //rolling back to history | |
pop ecx | |
pop ebx | |
pop eax | |
popf | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-1(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000;//setting the count | |
__asm | |
{ | |
pushf //backup | |
push eax | |
push ebx | |
push ecx | |
push edx | |
mov ecx,0 //init of loop range(0 to 200000000) | |
mov edx,j | |
mov eax,a //getting variables to registers | |
mov ebx,b | |
do_it_again2: //begin to loop | |
//swapping with using only 2 variables(only in cpu) | |
sub eax,ebx //a is now a-b | |
add ebx,eax //b is now a | |
sub eax,ebx //a is now -b | |
xor eax,80000000h //a is now b and four instructions total | |
inc ecx // j++ | |
cmp ecx,edx //i<200000000 ? | |
jb do_it_again2 // end of loop block | |
pop edx //rollback | |
pop ecx | |
pop ebx | |
pop eax | |
popf | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-2(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000;//setting the count | |
__asm | |
{ | |
xor ecx,j //init of loop range(200000000 to 0) | |
mov eax,a //getting variables to registers | |
mov ebx,b | |
do_it_again3: //begin to loop | |
//swapping with using only 2 variables(only in cpu) | |
xor eax,ebx | |
xor ebx,eax | |
xor eax,ebx | |
mov a,eax | |
mov b,ebx | |
dec ecx // j-- | |
jnz do_it_again3 // end of loop block | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-3(cycles) %li \n",(t2-t1)); | |
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
all: test # dm-test | |
CPPFLAGS+=-g -O3 -Wall -std=c99 -masm=intel | |
%:%.c | |
$(CC) $(CPPFLAGS) $^ -o $@ | |
dm-test: dm-test.c | |
dmc -O $^ |
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> | |
#include<stdlib.h> | |
#include<time.h> | |
int j=0; | |
int a=0,b=0,temp=0; | |
time_t t1=0; | |
time_t t2=0; | |
int main() | |
{ | |
srand(time(0)); | |
t1=clock(); | |
for(int i=0; i<200000000; i++) | |
{ | |
temp=a; | |
a=b; | |
b=temp; | |
} | |
t2=clock(); | |
printf("\n time of for-loop(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
while(j<200000000) | |
{ | |
temp=a; | |
a=b; | |
b=temp; | |
j++; | |
} | |
t2=clock(); | |
printf("\n time of while-loop(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000; | |
#define asm __asm | |
{ | |
asm(".intel_syntax noprefix\n"); | |
////asm("pushf\n"); | |
////asm("push eax\n"); | |
////asm("push ebx\n"); | |
////asm("push ecx\n"); | |
////asm("push edx\n"); | |
asm("mov ecx,0\n"); | |
asm("mov edx,j\n"); | |
asm("do_it_again:\n"); | |
asm("mov eax,a\n"); | |
asm("mov ebx,b\n"); | |
asm("mov b,eax\n"); | |
asm("mov a,ebx\n"); | |
asm("inc ecx\n"); | |
asm("cmp ecx,edx\n"); | |
asm("jb do_it_again\n"); | |
//asm("pop edx\n"); | |
//asm("pop ecx\n"); | |
//asm("pop ebx\n"); | |
//asm("pop eax\n"); | |
//asm("popf\n"); | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-1(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000; | |
{ | |
asm(".intel_syntax noprefix\n"); | |
////asm("pushf\n"); | |
////asm("push eax\n"); | |
////asm("push ebx\n"); | |
////asm("push ecx\n"); | |
////asm("push edx\n"); | |
asm("mov ecx,0\n"); | |
asm("mov edx,j\n"); | |
asm("mov eax,a\n"); | |
asm("mov ebx,b\n"); | |
asm("do_it_again2:\n"); | |
asm("sub eax,ebx\n"); | |
asm("add ebx,eax\n"); | |
asm("sub eax,ebx\n"); | |
asm("xor eax,0x80000000\n"); | |
asm("inc ecx\n"); | |
asm("cmp ecx,edx\n"); | |
asm("jb do_it_again2\n"); | |
//asm("pop edx\n"); | |
//asm("pop ecx\n"); | |
//asm("pop ebx\n"); | |
//asm("pop eax\n"); | |
//asm("popf\n"); | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-2(cycles) %li \n",(t2-t1)); | |
t1=clock(); | |
j=200000000; | |
{ | |
asm(".intel_syntax noprefix\n"); | |
asm("xor ecx,j\n"); | |
asm("mov eax,a\n"); | |
asm("mov ebx,b\n"); | |
asm("do_it_again3:\n"); | |
asm("xor eax,ebx\n"); | |
asm("xor ebx,eax\n"); | |
asm("xor eax,ebx\n"); | |
asm("mov a,eax\n"); | |
asm("mov b,ebx\n"); | |
asm("dec ecx\n"); | |
asm("jnz do_it_again3\n"); | |
} | |
t2=clock(); | |
printf("\n time of custom-loop-3(cycles) %li \n",(t2-t1)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment