How to read 4GB file on 32bit system

前端 未结 4 1797
予麋鹿
予麋鹿 2020-12-19 14:53

In my case I have different files lets assume that I have >4GB file with data. I want to read that file line by line and process each line. One of my restrictions is that so

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 15:24

    Nice to see you found my benchmark at How to parse space-separated floats in C++ quickly?

    It seems you're really looking for the fastest way to count lines (or any linear single pass analysis), I've done a similar analysis and benchmark of exactly that here

    • Fast textfile reading in c++

    Interestingly, you'll see that the most performant code does not need to rely on memory mapping at all there.

    static uintmax_t wc(char const *fname)
    {
        static const auto BUFFER_SIZE = 16*1024;
        int fd = open(fname, O_RDONLY);
        if(fd == -1)
            handle_error("open");
    
        /* Advise the kernel of our access pattern.  */
        posix_fadvise(fd, 0, 0, 1);  // FDADVICE_SEQUENTIAL
    
        char buf[BUFFER_SIZE + 1];
        uintmax_t lines = 0;
    
        while(size_t bytes_read = read(fd, buf, BUFFER_SIZE))
        {
            if(bytes_read == (size_t)-1)
                handle_error("read failed");
            if (!bytes_read)
                break;
    
            for(char *p = buf; (p = (char*) memchr(p, '\n', (buf + bytes_read) - p)); ++p)
                ++lines;
        }
    
        return lines;
    }
    

提交回复
热议问题