Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "time.h"
- void f0(int *x){
- *x += 1;
- }
- void f1(int *x){
- *x += 2;
- }
- void f2(int *x){
- *x += 3;
- }
- void f3(int *x){
- *x += 4;
- }
- void f4(int *x){
- *x += 5;
- }
- void f5(int *x){
- *x += 6;
- }
- void f6(int *x){
- *x += 7;
- }
- void f7(int *x){
- *x += 8;
- }
- void f8(int *x){
- *x += 9;
- }
- void f9(int *x){
- *x += 10;
- }
- void f10(int *x){
- *x -= 1;
- }
- void f11(int *x){
- *x -= 2;
- }
- void f12(int *x){
- *x -= 3;
- }
- void f13(int *x){
- *x -= 4;
- }
- void f14(int *x){
- *x -= 5;
- }
- void f15(int *x){
- *x -= 6;
- }
- void f16(int *x){
- *x -= 7;
- }
- void f17(int *x){
- *x -= 8;
- }
- void f18(int *x){
- *x -= 9;
- }
- void f19(int *x){
- *x -= 10;
- }
- void f20(int *x){
- *x *= 1;
- }
- void f21(int *x){
- *x *= 2;
- }
- void f22(int *x){
- *x *= 3;
- }
- void f23(int *x){
- *x *= 4;
- }
- void f24(int *x){
- *x *= 5;
- }
- void f25(int *x){
- *x /= 6;
- }
- void f26(int *x){
- *x /= 5;
- }
- void f27(int *x){
- *x /= 4;
- }
- void f28(int *x){
- *x /= 3;
- }
- void f29(int *x){
- *x /= 2;
- }
- #define TEST_ITTERATIONS 100000000
- #define RAND_COUNT 2000
- int randi[RAND_COUNT];
- #define FUNC_COUNT 30
- void (*func[])(int*) = {
- f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,f25,f26,f27,f28,f29
- };
- void set_rand(){
- srand (time(NULL)); // Seed random
- for (int i = 0; i < RAND_COUNT; i++){
- randi[i] = rand() % FUNC_COUNT;
- }
- }
- void test_pointers(){
- int x = 0;
- for (int i = 0; i < TEST_ITTERATIONS; i++){
- func[randi[i % RAND_COUNT]](&x);
- }
- printf("Pointer result: %d\n", x);
- }
- void test_switch(){
- int x = 0;
- for (int i = 0; i < TEST_ITTERATIONS; i++){
- switch (randi[i % RAND_COUNT]){
- case 0:
- f0(&x);
- break;
- case 1:
- f1(&x);
- break;
- case 2:
- f2(&x);
- break;
- case 3:
- f3(&x);
- break;
- case 4:
- f4(&x);
- break;
- case 5:
- f5(&x);
- break;
- case 6:
- f6(&x);
- break;
- case 7:
- f7(&x);
- break;
- case 8:
- f8(&x);
- break;
- case 9:
- f9(&x);
- break;
- case 10:
- f10(&x);
- break;
- case 11:
- f11(&x);
- break;
- case 12:
- f12(&x);
- break;
- case 13:
- f13(&x);
- break;
- case 14:
- f14(&x);
- break;
- case 15:
- f15(&x);
- break;
- case 16:
- f16(&x);
- break;
- case 17:
- f17(&x);
- break;
- case 18:
- f18(&x);
- break;
- case 19:
- f19(&x);
- break;
- case 20:
- f20(&x);
- break;
- case 21:
- f21(&x);
- break;
- case 22:
- f22(&x);
- break;
- case 23:
- f23(&x);
- break;
- case 24:
- f24(&x);
- break;
- case 25:
- f25(&x);
- break;
- case 26:
- f26(&x);
- break;
- case 27:
- f27(&x);
- break;
- case 28:
- f28(&x);
- break;
- case 29:
- f29(&x);
- break;
- }
- }
- printf("switch result: %d\n", x);
- }
- int main(){
- printf("Itterations: %d\n", TEST_ITTERATIONS);
- set_rand();
- float startTime;
- float endTime;
- float timeElapsed;
- startTime = (float)clock()/CLOCKS_PER_SEC;
- test_switch();
- endTime = (float)clock()/CLOCKS_PER_SEC;
- timeElapsed = endTime - startTime;
- printf("Switch time: %f\n", timeElapsed);
- startTime = (float)clock()/CLOCKS_PER_SEC;
- test_pointers();
- endTime = (float)clock()/CLOCKS_PER_SEC;
- timeElapsed = endTime - startTime;
- printf("Pointer time: %f\n", timeElapsed);
- return 0;
- }
Add Comment
Please, Sign In to add comment