-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiling.cpp
More file actions
63 lines (63 loc) · 1.76 KB
/
Profiling.cpp
File metadata and controls
63 lines (63 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <time.h>
#include <math.h>
#define iterations 100000000
#define iters 100
/*
Write a program to print even numbers from 0 to 100 using a loop.
Measure and display the execution time taken by the program to complete this task using clock() function.
*/
void function1(){
for (int number = 0; number <= iterations; number += 2) {
// printf("%d\n", number);
}
}
void function2(){
int number = 0;
while (number < iterations) {
if (number >> 1 == 0) {
// printf("%d\n", number);
}
number++;
}
}
void function3(){
for (int number = 0; number < iterations; number++) {
if (number / 2 == 0) {
// printf("%d\n", number);
}
}
}
double average(int a[iters]){
int sum = 0;
for (int i = 0; i < iters; i++) {
sum += a[i];
}
return sum / iters;
}
int main() {
int t[iters];
for(int i = 0; i < iters; i++){
clock_t start_time, end_time;
double cpu_time_used;
start_time = clock();
function3();
end_time = clock();
cpu_time_used = ((double) (end_time - start_time));
t[i] = cpu_time_used;
// printf("Total clock ticks used: %ld\n", (long)(end_time - start_time));
}
printf("Average time taken by function1: %f seconds\n", average(t));
for(int i = 0; i < iters; i++){
clock_t start_time, end_time;
double cpu_time_used;
start_time = clock();
function2();
end_time = clock();
cpu_time_used = ((double) (end_time - start_time));
t[i] = cpu_time_used;
// printf("Total clock ticks used: %ld\n", (long)(end_time - start_time));
}
printf("Average time taken by function1: %f seconds\n", average(t));
return 0;
}