klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
| 1 | #include <stdio.h> |
| 2 | #include <time.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include "kseq.h" |
| 8 | KSTREAM_INIT(int, read, 4096) |
| 9 | |
| 10 | #define BUF_SIZE 65536 |
| 11 | |
| 12 | int main(int argc, char *argv[]) |
| 13 | { |
| 14 | clock_t t; |
| 15 | if (argc == 1) { |
| 16 | fprintf(stderr, "Usage: %s <in.txt>\n", argv[0]); |
| 17 | return 1; |
| 18 | } |
| 19 | { |
| 20 | FILE *fp; |
| 21 | char *s; |
| 22 | t = clock(); |
| 23 | s = malloc(BUF_SIZE); |
| 24 | fp = fopen(argv[1], "r"); |
| 25 | while (fgets(s, BUF_SIZE, fp)); |
| 26 | fclose(fp); |
| 27 | fprintf(stderr, "[fgets] %.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); |
| 28 | } |
| 29 | { |
| 30 | int fd, dret; |
| 31 | kstream_t *ks; |
| 32 | kstring_t s; |
| 33 | t = clock(); |
| 34 | s.l = s.m = 0; s.s = 0; |
| 35 | fd = open(argv[1], O_RDONLY); |
| 36 | ks = ks_init(fd); |
| 37 | while (ks_getuntil(ks, '\n', &s, &dret) >= 0); |
| 38 | free(s.s); |
| 39 | ks_destroy(ks); |
| 40 | close(fd); |
| 41 | fprintf(stderr, "[kstream] %.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); |
| 42 | } |
| 43 | return 0; |
| 44 | } |