Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- void while_loop(int target) {
- int i = 0;
- while (i < target) {
- printf("%d\n", i);
- i++;
- }
- }
- void for_loop(int target) {
- for (int i = 0; i < target; i++) {
- printf("%d\n", i);
- }
- }
- void do_loop(int target) {
- int i = 0;
- do {
- printf("%d\n", i);
- i++;
- } while (i < target);
- }
- int main() {
- int target = 0;
- printf("Enter the target amount: ");
- scanf("%d", &target);
- printf("What function do you want to measure?\n\n");
- printf("1. While loop\n");
- printf("2. For loop\n");
- printf("3. Do loop\n");
- printf("4. Exit\n\n");
- int choice = 0;
- printf("Enter your choice: ");
- scanf("%d", &choice);
- clock_t start = NULL;
- switch (choice) {
- case 1:
- start = clock();
- while_loop(target);
- break;
- case 2:
- start = clock();
- for_loop(target);
- break;
- case 3:
- start = clock();
- do_loop(target);
- break;
- case 4:
- break;
- default:
- printf("Invalid choice\n");
- break;
- }
- if (start != NULL) {
- clock_t end = clock();
- double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
- printf("Time spent: %f\n", time_spent);
- } else {
- printf("No time spent\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment