Fastest way to read every 30th byte of large binary file?

前端 未结 7 1750

What is the fastest way to read every 30th byte of a large binary file (2-3 GB)? I\'ve read there are performance problems with fseek because of I/O buffers, but I don\'t wa

7条回答
  •  眼角桃花
    2020-12-24 14:45

    If you're willing to break out of ANSI-C and use OS specific calls, I'd recommend using memory mapped files. This is the Posix version (Windows has it's own OS specific calls):

    #define MAPSIZE 4096
    int fd = open(file, O_RDONLY);
    struct stat stbuf;
    fstat(fd, &stbuf);
    
    
    char *addr = 0;
    off_t last_mapped_offset = -1;
    off_t idx = 0;
    while (idx < stbuf.st_size)
    {
        if (last_mapped_offset != (idx / MAPSIZE))
        {
            if (addr)
                munmap(addr, MAPSIZE);
    
            last_mapped_offset = idx / MAPSIZE; 
    
            addr = mmmap(0, MAPSIZE, PROT_READ, MAP_FILE, fd, idx, last_mapped_offset);
        }
    
        *(addr + (idx % MAPSIZE));
    
        idx += 30;
    
    }
    
    munmap(addr, MAPSIZE);
    close(fd);
    

提交回复
热议问题