/* Linearly search utlfile/padr/SELE-SAL-CONSTA.p20out1.20230415.tmp * for Daniel Osmar Maidana’s CUIT. A microbenchmark; takes 102 ms on * my MicroPC to search 6.25 million records. See also columncuits.c * and findcuit.c. */ #include #include #include #include #include #include #include enum { recsize = 52 }; int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s utlfile/padr/SELE-SAL-CONSTA.p20out1.20230415.tmp\n", argv[0]); return 1; } int fd = open(argv[1], O_RDONLY); if (fd < 0) { perror(argv[1]); return -1; } off_t size = lseek(fd, 0, SEEK_END); printf("%s is %ld bytes\n", argv[1], size); char *mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t)0); if (mem == MAP_FAILED) { perror("mmap"); return 1; } close(fd); char buf[recsize+1] = {0}; /* for (int off = 0; off < size; off = off * 2 + recsize) { */ /* memcpy(buf, mem + off, recsize); */ /* printf("%12d %s", off, buf); */ /* } */ char key[] = "23304663359"; for (int off = 0; off <= size - recsize; off += recsize) { if (0 == memcmp(key, mem + off, sizeof(key) - 1)) { memcpy(buf, mem + off, recsize); printf("%12d %s", off, buf); } } return 0; }