-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusy-loop.c
50 lines (41 loc) · 897 Bytes
/
busy-loop.c
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
/*
* Copyright 2018 Sandipan Das, IBM Corporation
*
* This uses simple loops to make the execution
* time of a function vary based on its arguments.
*
*/
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int foo(int x)
{
int i;
/*
* For different values of x, the execution
* time of foo() will vary
*/
if (x == 0) {
for (i = 0; i < INT8_MAX / 2; ++i);
} else if (x == 1) {
for (i = 0; i < INT16_MAX / 2; ++i);
} else if (x == 2) {
for (i = 0; i < INT32_MAX / 2; ++i);
}
return 0;
}
int main(int argc, char **argv)
{
int i, x;
/* Setup seed value */
srand(time(NULL));
for (i = 0; i < INT32_MAX; i++) {
/* Pick a random value */
x = rand() % 3;
/* Call foo() */
printf("call foo(%d)\n", x);
foo(x);
}
return EXIT_SUCCESS;
}