Reputation: 5247
I have two queries about the tie::file module
I have used the tie::file module to do a search on a 55 MB file and set an memory of 20 MB in tie::file. When i tried to grep on the tied array for a search string it's taking a lot of time. Is there any work around for it?
Can tie::file used for reading a binary file. The tied array is delimited by "\n". How do i use a tie::file to read an binary file? Could you pls paste me some sample code.
/home/a814899> perl -e 'print "x\n"x27 for 1..1024*1024;' >a
/home/a814899> echo "hello world" >> a
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)
Upvotes: 0
Views: 403
Reputation: 386311
Yes.
This is how you probably did it using 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
This is the "workaround":
$ (
time perl -e'
while (<>) { if (/y/) { } }
' a
) 2>&1 | grep real
real 0m0.644s
The data file was created using
$ perl -E'say "x"x54 for 1..1024*1024;' >a
Tie::File doesn't read files; Tie::File provides a means of mapping lines of a file to array elements. Since "binary" files have no lines, accessing one using Tie::File wouldn't make any sense.
Upvotes: 2