/* Measure the time to sum a file of native-byte-order floating-point * numbers, as a kind of upper bound on how fast brute force can be. */ #include #include #include #include #include #include #include void die(char *why) { perror(why); abort(); } int main(int argc, char **argv) { int fd = open(argv[1], O_RDONLY); if (fd < 0) die(argv[1]); struct stat st; if (fstat(fd, &st) < 0) die("fstat"); size_t size = st.st_size; char *mapped = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); if (MAP_FAILED == mapped) die("mmap"); float total = 0; size_t max_idx = size / sizeof(total); for (int i = 0; i < max_idx; i++) { total += ((float*)mapped)[i]; } printf("%ld items totaling %f\n", (long)max_idx, total); return 0; }