我有两个关于tie :: file module
的问题我使用tie :: file模块在55 MB文件上进行搜索,并在tie :: file中设置20 MB的内存。当我试图在绑定数组上搜索一个搜索字符串时,需要花费很多时间。是否有任何解决方法?
可以绑定::用于读取二进制文件的文件。绑定数组由“\ n”分隔。我如何使用tie :: file来读取二进制文件?你能不能给我贴一些示例代码。
/家庭/ a814899> perl -e'print“x \ n”x27 for 1..1024 * 1024;' >一种
/家庭/ a814899> echo“hello world”>>一个
Using Unix grep
/home/a814899> time grep "hello " a
hello world
real 0m8.280s
user 0m8.129s
sys 0m0.139s
Using the regex
/home/a814899> (time perl -e 'while (<>) { if (/hello/) { print "hello world"} }' a)
hello world
real 0m51.316s
user 0m51.087s
sys 0m0.189s
Using Perl Grep
#!/usr/bin/perl
print "executing\n";
my $outputFileDir="/home/a814899";
my $sFileName="a";
open my $fh, "<", $outputFileDir . "/" . $sFileName or do {
print "Could not open the file";
};
print "success in open" . "\n";
my @out=grep {/hello world/} <$fh> ;
print "@out" ;
close($fh)
答案 0 :(得分:2)
是
这就是你使用Tie :: File:
的方法$ (
time perl -MTie::File -e'
tie @a, "Tie::File", $ARGV[0];
for (@a) { if (/y/) { } }
' a
) 2>&1 | grep real
real 2m44.333s
这是“解决方法”:
$ (
time perl -e'
while (<>) { if (/y/) { } }
' a
) 2>&1 | grep real
real 0m0.644s
数据文件是使用
创建的$ perl -E'say "x"x54 for 1..1024*1024;' >a
Tie :: File不读取文件; Tie :: File提供了将文件行映射到数组元素的方法。由于“二进制”文件没有行,使用Tie :: File访问一个文件没有任何意义。