-
Notifications
You must be signed in to change notification settings - Fork 11
/
benchco.c
67 lines (53 loc) · 1.56 KB
/
benchco.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "coroutine.h"
#ifdef __APPLE__
# error Mac OS-X does not support clock_gettime()
#endif
static uintptr_t subtask(coroutine__s *self,uintptr_t data)
{
while(data--)
data = coroutine_yield(self,data);
return 0;
}
/*************************************************************************/
static void runtest(struct timespec *result,coroutine__s *co,uintptr_t data)
{
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_MONOTONIC,&start);
while(data--)
data = coroutine_yield(co,data);
clock_gettime(CLOCK_MONOTONIC,&end);
if (end.tv_nsec < start.tv_nsec)
{
end.tv_nsec += 1000000000L;
end.tv_sec--;
}
result->tv_nsec = end.tv_nsec - start.tv_nsec;
result->tv_sec = end.tv_sec - start.tv_sec;
}
/*************************************************************************/
int main(int argc,char *argv[])
{
struct timespec total;
coroutine__s *co;
uintptr_t max;
unsigned long long bav;
long res;
if (argc == 1)
max = 1000000uL;
else
max = strtoul(argv[1],NULL,10);
coroutine_create(&co,0,subtask);
runtest(&total,co,max);
bav = (unsigned long long)total.tv_sec * 1000000000uLL
+ (unsigned long long)total.tv_nsec;
res = (unsigned long)(bav / (unsigned long long)max);
res /= 2; /* adjust for both coroutines adjusting count */
printf("Initial coroutine_yield() %ld %ld %ld\n",(long)total.tv_sec,(long)total.tv_nsec,res);
coroutine_free(co);
return 0;
}