s3r_rea@ararat:~/c/test1> gcc t.c -O3
s3r_rea@ararat:~/c/test1> ./a.out
LOX is testing...
G_i: 10029202
Time: 2974 ms.
FP array is testing...
G_i: 20027413
Time: 3026 ms.
s3r_rea@ararat:~/c/test1> cat t.c
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
static int G_i=0;
typedef void (*cmd)();
void cmd1(){ G_i += 1; }
void cmd2(){ G_i -= 1; }
void cmd3(){ G_i += 2; }
void cmd4(){ G_i -= 2; }
void cmd5(){ G_i += 3; }
void cmd6(){ G_i -= 3; }
void cmd7(){ G_i += 4; }
void cmd8(){ G_i -= 4; }
void cmd9(){ G_i += 1; }
void cmd11(){ G_i += 1; }
void cmd12(){ G_i -= 1; }
void cmd13(){ G_i += 2; }
void cmd14(){ G_i -= 2; }
void cmd15(){ G_i += 3; }
void cmd16(){ G_i -= 3; }
void cmd17(){ G_i += 4; }
void cmd18(){ G_i -= 4; }
void cmd19(){ G_i += 1; }
static cmd aCmd[] = {
&cmd1, &cmd2, &cmd3, &cmd4, &cmd5, &cmd6, &cmd7, &cmd8, &cmd9,
&cmd11, &cmd12, &cmd13, &cmd14, &cmd15, &cmd16, &cmd17, &cmd18, &cmd19
};
void LOX(int i){
switch(i){
case 0: cmd1(); break;
case 1: cmd2(); break;
case 2: cmd3(); break;
case 3: cmd4(); break;
case 4: cmd5(); break;
case 5: cmd6(); break;
case 6: cmd7(); break;
case 7: cmd8(); break;
case 8: cmd9(); break;
case 9: cmd11(); break;
case 10: cmd12(); break;
case 11: cmd13(); break;
case 12: cmd14(); break;
case 13: cmd15(); break;
case 14: cmd16(); break;
case 15: cmd17(); break;
case 16: cmd18(); break;
case 17: cmd19(); break;
default: break;
}
}
long long getMS(const struct timeval *start, const struct timeval *end){
long long seconds, useconds;
seconds = end->tv_sec - start->tv_sec;
useconds = end->tv_usec - start->tv_usec;
return ((seconds) * 1000 + useconds/1000.0) + 0.5;
}
int main(){
struct timeval start, end;
long long mtime;
srand (time (NULL));
int i, j, Max = 10000000;
printf("LOX is testing...\n");
gettimeofday(&start, NULL);
for(i=0; i < Max; i++)
for(j = 0; j < 9; j++)
LOX(rand () % 18);
gettimeofday(&end, NULL);
printf("G_i: %d \n", G_i);
printf("Time: %lld ms. \n\n", getMS(&start, &end));
printf("FP array is testing...\n ");
gettimeofday(&start, NULL);
for(i=0; i < Max; i++)
for(j = 0; j < 9; j++)
(*(aCmd + (rand () % 18)))();
gettimeofday(&end, NULL);
printf("G_i: %d \n", G_i);
printf("Time: %lld ms. \n\n", getMS(&start, &end));
return 0;
}
s3r_rea@ararat:~/c/test1> ls
a.out t.c
s3r_rea@ararat:~/c/test1>
Забавно, не правда ли?