DUZENLEME: (Orjinal metin asagidadir)
Bu test kodu
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
#include<stddef.h>
#include<time.h>
#include <unistd.h>
#include <sys/time.h>
int64_t naiv_bolme(int64_t a , int64_t b)
{
int64_t i;
i=0;
while(a>=b)
{
a=a-b;
i++;
}
return i;
}
int main(){
srand(time(NULL));
int64_t MO = 6431238016;
int64_t res1 = 1;
int64_t res2 = 2;
time_t start = time(NULL);
double time_naiv = 0 ;
double time_builtin = 0 ;
int64_t j = 1;
for(int64_t i = 1 ; i< INT64_MAX ; i+= MO){
printf(" %ld / %ld \n",i,j);
start = time(NULL);
res2 = i/ j;
time_builtin = (double)(time(NULL) - start);
printf(" builtin %lf \n ",time_builtin);
start = time(NULL);
res1 = naiv_bolme(i, j);
time_naiv = (double)(time(NULL) - start);
printf(" naiv %lf \n",time_naiv);
}
exit(0);
}
Bu da sonuclar
~ $ ./a.out
1 / 1
builtin 0.000000
naiv 0.000000
6431238017 / 1
builtin 0.000000
naiv 11.000000
12862476033 / 1
builtin 0.000000
naiv 23.000000
19293714049 / 1
builtin 0.000000
naiv 34.000000
25724952065 / 1
builtin 0.000000
naiv 46.000000
32156190081 / 1
builtin 0.000000
naiv 58.000000
38587428097 / 1
builtin 0.000000
naiv 71.000000
45018666113 / 1
builtin 0.000000
naiv 81.000000
51449904129 / 1
builtin 0.000000
naiv 92.000000
57881142145 / 1
builtin 0.000000
Sonunda beklemeye usendim
__________________________________________________________
(ORJINAL METIN)
MO
yu 1 e esitlediginizde takilan kod (derlerken (compile)-O0
bayragi(flag) ile derlemelisiniz yoksa derleyeciniz ne yapmaya calistiginizi anlayip optimize eder)
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
#include<stddef.h>
#include<time.h>
#include <unistd.h>
#include <sys/time.h>
int64_t naiv_bolme(int64_t a , int64_t b)
{
int64_t i;
i=0;
while(a>=b)
{
a=a-b;
i++;
}
return i;
}
int main(){
srand(time(NULL));
int64_t MAX_ITER = 1024;
int64_t MO = 6431238016;
int64_t res1 = 1;
int64_t res2 = 2;
time_t start = time(NULL);
double time_naiv = 0 ;
double time_builtin = 0 ;
for(int64_t j = 1 ; j < MAX_ITER ; j++){
for(int64_t i = INT64_MAX/MO ; i > INT64_MAX/MO - j ; i--){
printf(" %ld / %ld \n",i,j);
start = time(NULL);
res2 = i/ j;
time_builtin = (double)(time(NULL) - start);
printf(" builtin %lf \n ",time_builtin);
start = time(NULL);
res1 = naiv_bolme(i, j);
time_naiv = (double)(time(NULL) - start);
printf(" naiv %lf \n",time_naiv);
}
}
exit(0);
}
biraz uzun oldu ama kisaca MAX_INT
i kucuk bir sayiya bolmeye calisin arasindaki hiz farkini anlayacaksiniz