Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<time.h>
- #define VAL_ONE 10
- #define VAL_TWO 5
- #define TOTAL_ITERATIONS 1000000
- // Functional pointer
- void (*with_test_pointer)(int *) = NULL;
- void with_xor(int *value)
- {
- *value ^= VAL_ONE ^ VAL_TWO;
- }
- void with_conditional(int *value)
- {
- *value = (*value == VAL_ONE)? VAL_TWO : VAL_ONE;
- }
- void with_if(int *value)
- {
- if(VAL_ONE == *value)
- {
- *value = VAL_TWO;
- }
- else
- {
- *value = VAL_ONE;
- }
- }
- void run_test(void (*with_test_pointer)(int*), double *time_spent)
- {
- int i;
- clock_t start, end;
- int a = VAL_ONE;
- start = clock();
- for(i = 0; i < TOTAL_ITERATIONS; i++)
- {
- (*with_test_pointer)(&a);
- }
- end = clock();
- *time_spent = ((double) (end - start))/ CLOCKS_PER_SEC;
- }
- int main()
- {
- int i;
- double diff;
- double diff_xor = 0;
- double diff_if = 0;
- double diff_conditional = 0;
- for(i = 0; i < 10; i++)
- {
- run_test(&with_xor, &diff);
- diff_xor += diff;
- run_test(&with_if, &diff);
- diff_if += diff;
- run_test(&with_conditional, &diff);
- diff_conditional += diff;
- }
- printf("xor %f\t\t\t\n", diff_xor);
- printf("conditional %f\t\t\t\n", diff_conditional);
- printf("if %f\t\t\t\n", diff_if);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement