读取和写入同一文件时的 Perl 性能
Perl performance when reading and writing the same file
在 Linux 上,使用 Perl reading/writing 用户文件的这两种方式之间是否存在明显的性能差异?
选项 1:
open (READFILE, '<:utf8', "users/$_[0]") or die ("no read users/$_[0]");
# Do the reading
close (READFILE) or die;
# Do more stuff
open (WRITEFILE, '>:utf8', "users/$_[0]") or die ("no write users/$_[0]"); flock (WRITEFILE, 2) or die ("no lock users/$_[0]");
# Do the writing
close (WRITEFILE) or die;
选项 2:
open (USERFILE, '+<:utf8', "users/$_[0]") or die ("no open users/$_[0]"); flock (USERFILE, 2) or die ("no lock users/$_[0]");
# Do the reading
# Do more stuff
seek (USERFILE, 0, 0); truncate (USERFILE, 0);
# Do the writing
close (USERFILE) or die ("no write users/$_[0]");
用户文件不大,通常为 20-40 行或每行 2-4 KB。
选择选项 1 或 2(或第三个选项)还有其他原因吗?
这是您可以用来测试它的基准,我怀疑如果您 close
然后再次 open
获取新的文件描述符是需要更长时间的部分。
#!/usr/bin/env perl
use warnings;
use strict;
use open qw(:encoding(utf8) :std);
use Benchmark qw<cmpthese>;
my $text = <<TEXT;
I had some longer text here, but for better readability, just
these two lines.
TEXT
cmpthese(10_000,{
close => sub{
open my $file, '<',"bla" or die "$!";
my @array = <$file>;
close $file or die;
open $file, '>',"bla" or die "$!";
$file->print($text)
},
truncate => sub {
open my $file, '+<',"bla" or die "$!";
my @array = <$file>;
seek $file,0,0;
truncate $file, 0;
$file->print($text)
},
truncate_flock => sub {
open my $file, '+<',"bla" or die "$!";
flock $file, 2;
my @array = <$file>;
seek $file,0,0;
truncate $file, 0;
$file->print($text)
},
});
我机器上的输出:
Rate close truncate_flock truncate
close 2703/s -- -15% -17%
truncate_flock 3175/s 17% -- -3%
truncate 3257/s 21% 3% --
比率越高越好。使用 close
慢 1.17 倍。
但这在很大程度上取决于您的 more stuff
需要多长时间,因为您在 truncate
示例中 flock
ing 文件,如果另一个程序试图访问该文件,它可能会变慢因为那个。
在 Linux 上,使用 Perl reading/writing 用户文件的这两种方式之间是否存在明显的性能差异?
选项 1:
open (READFILE, '<:utf8', "users/$_[0]") or die ("no read users/$_[0]");
# Do the reading
close (READFILE) or die;
# Do more stuff
open (WRITEFILE, '>:utf8', "users/$_[0]") or die ("no write users/$_[0]"); flock (WRITEFILE, 2) or die ("no lock users/$_[0]");
# Do the writing
close (WRITEFILE) or die;
选项 2:
open (USERFILE, '+<:utf8', "users/$_[0]") or die ("no open users/$_[0]"); flock (USERFILE, 2) or die ("no lock users/$_[0]");
# Do the reading
# Do more stuff
seek (USERFILE, 0, 0); truncate (USERFILE, 0);
# Do the writing
close (USERFILE) or die ("no write users/$_[0]");
用户文件不大,通常为 20-40 行或每行 2-4 KB。
选择选项 1 或 2(或第三个选项)还有其他原因吗?
这是您可以用来测试它的基准,我怀疑如果您 close
然后再次 open
获取新的文件描述符是需要更长时间的部分。
#!/usr/bin/env perl
use warnings;
use strict;
use open qw(:encoding(utf8) :std);
use Benchmark qw<cmpthese>;
my $text = <<TEXT;
I had some longer text here, but for better readability, just
these two lines.
TEXT
cmpthese(10_000,{
close => sub{
open my $file, '<',"bla" or die "$!";
my @array = <$file>;
close $file or die;
open $file, '>',"bla" or die "$!";
$file->print($text)
},
truncate => sub {
open my $file, '+<',"bla" or die "$!";
my @array = <$file>;
seek $file,0,0;
truncate $file, 0;
$file->print($text)
},
truncate_flock => sub {
open my $file, '+<',"bla" or die "$!";
flock $file, 2;
my @array = <$file>;
seek $file,0,0;
truncate $file, 0;
$file->print($text)
},
});
我机器上的输出:
Rate close truncate_flock truncate
close 2703/s -- -15% -17%
truncate_flock 3175/s 17% -- -3%
truncate 3257/s 21% 3% --
比率越高越好。使用 close
慢 1.17 倍。
但这在很大程度上取决于您的 more stuff
需要多长时间,因为您在 truncate
示例中 flock
ing 文件,如果另一个程序试图访问该文件,它可能会变慢因为那个。