How can I set the file-read buffer size in Perl to optimize it for large files?

前端 未结 4 604
暗喜
暗喜 2021-01-05 06:15

I understand that both Java and Perl try quite hard to find a one-size-fits all default buffer size when reading in files, but I find their choices to be increasingly antiqu

4条回答
  •  情深已故
    2021-01-05 06:49

    No, there's not (short of recompiling a modified perl), but you can read the whole file into memory, then work line by line from that:

    use File::Slurp;
    my $buffer = read_file("filename");
    open my $in_handle, "<", \$buffer;
    while ( my $line = readline($in_handle) ) {
    }
    

    Note that perl before 5.10 defaulted to using stdio buffers in most places (but often cheating and accessing the buffers directly, not through the stdio library), but in 5.10 and later defaults to its own perlio layer system. The latter seems to use a 4k buffer by default, but writing a layer that allows configuring this should be trivial (once you figure out how to write a layer: see perldoc perliol).

提交回复
热议问题