]>
git.saurik.com Git - apple/libdispatch.git/blob - testing/float_parsing.c
1 #include <dispatch/dispatch.h>
14 #define MAXLAPS (512 * 1024)
17 test(size_t LAPS
, char *nums
)
19 uint64_t concurrent_cycles
;
20 uint64_t serial_cycles
;
21 char **result_strings
;
26 result_strings
= calloc(1, sizeof(char *) * LAPS
);
27 assert(result_strings
);
29 results
= calloc(1, sizeof(double) * LAPS
);
32 printf("%zu random floats\n", LAPS
);
37 result_strings
[i
] = nums_off
;
41 } while (*nums_off
!= '\n');
46 for (i
= 0; i
< LAPS
; i
++) {
47 assert(result_strings
[i
]);
50 concurrent_cycles
= dispatch_benchmark(10, ^{
51 dispatch_apply(LAPS
, dispatch_get_concurrent_queue(0), ^(size_t idx
) {
52 results
[idx
] = strtod(result_strings
[idx
], NULL
);
56 for (i
= 0; i
< LAPS
; i
++) {
60 serial_cycles
= dispatch_benchmark(10, ^{
63 results
[k
] = strtod(result_strings
[k
], NULL
);
68 for (i
= 0; i
< LAPS
; i
++) {
72 printf( "\tserial cycles:\t%llu\n"
73 "\tapply() cycles:\t%llu\n"
74 "\tserial / concurrent: %.2Lf\n",
75 serial_cycles
, concurrent_cycles
,
76 (long double)serial_cycles
/ (long double)concurrent_cycles
);
85 char path
[PATH_MAX
] = "/tmp/random_floats_XXXXXX";
94 rfd
= open("/dev/random", O_RDONLY
);
105 r
= read(rfd
, words
, sizeof(words
));
106 assert(r
== sizeof(words
));
107 for (j
= 0; j
< 1000; j
++) {
108 if (isnormal(words
[j
])) {
109 r
= write(fd
, buf
, snprintf(buf
, sizeof(buf
), "%.20e\n", words
[j
]));
114 } while (i
< MAXLAPS
);
122 nums
= mmap(NULL
, sb
.st_size
, PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
, 0);
123 assert(nums
!= MAP_FAILED
);
125 for (i
= MAXLAPS
; i
> 0; i
/= 2) {
129 r
= munmap(nums
, sb
.st_size
);