#include #include void print0() { for (int i = 0; i < 100; i++) { int a = i * i; } // printf("You chose zero.\n"); } void print1() { for (int i = 0; i < 100; i++) { int a = i * i; } // printf("You chose one.\n"); } void print2() { for (int i = 0; i < 100; i++) { int a = i * i; } // printf("You chose two.\n"); } int main() { // Array of function pointers (jump table) void (*operations[3])() = {print0, print1, print2}; struct timespec start, end; long long total_time = 0; int num_iterations = 1000; clock_gettime(CLOCK_MONOTONIC, &start); // Start time for (int i = 0; i < num_iterations; i++) { int choice = i % 3; // Simulate user choices (0, 1, 2) // Call the function pointed to by operations[choice] (*operations[choice])(); } clock_gettime(CLOCK_MONOTONIC, &end); // End time // Compute the total time in nanoseconds total_time = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec); printf("Total time for %d iterations: %lld nanoseconds\n", num_iterations, total_time); printf("Average time per iteration: %lld nanoseconds\n", total_time / num_iterations); // Implement using switch case and measure time clock_gettime(CLOCK_MONOTONIC, &start); // Start time for (int i = 0; i < num_iterations; i++) { int choice = i % 3; // Simulate user choices (0, 1, 2) // Call the function pointed to by operations[choice] switch (choice) { case 0: print0(); break; case 1: print1(); break; case 2: print2(); break; } } clock_gettime(CLOCK_MONOTONIC, &end); // End time total_time = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec); printf("Total time for %d iterations using switch case: %lld nanoseconds\n", num_iterations, total_time); printf("Average time per iteration using switch case: %lld nanoseconds\n", total_time / num_iterations); return 0; }