-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitch_statement.c
More file actions
66 lines (61 loc) · 1.02 KB
/
switch_statement.c
File metadata and controls
66 lines (61 loc) · 1.02 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
64
65
66
#include "threaded_code.h"
typedef enum{
begin,
incr_i,
print_i,
branch,
loop,
quit
} program;
void run_program(unsigned ip, unsigned i, program *prog)
{
for (;;) {
switch (prog[ip]) {
case begin:
printf("Starting run:.\n");
break;
case incr_i:
i += 1;
break;
case print_i:
if (i % PRINT_INTERVAL == 0) {
printf("* ");
fflush(stdout);
}
break;
case branch:
if (i == MAX_ITERATIONS)
ip += 1;
break;
case loop:
ip = 0;
break;
case quit:
return;
}
ip = ip + 1;
}
}
int main(void)
{
program prog[] = {
begin,
incr_i,
print_i,
branch,
loop,
quit
};
printf("Threaded code demo: switch statement.\n");
double average = 0;
for(int j = 0; j < SAMPLE_NUM; ++j) {
time_t start_time = time(NULL);
run_program(0, 0, prog);
time_t end_time = time(NULL);
average += (end_time - start_time);
printf("\nTime elapsed: %lus\n", end_time - start_time);
}
average /= SAMPLE_NUM;
printf("Average of %d results: %f s\n\n", SAMPLE_NUM, average);
return 0;
}