Yes, appending to the index should cause no problems at all. As your limiting yourself to under 4 GB, then using pack 'J' (perl native unsigned 32-bit which saves a little conversion) rather 'd' effects a speedup of the indexing of around x4 giving under 5 seconds for the 1e6 file.
#! perl -slw
use strict;
$| = 1;
open FILE, '<', $ARGV[ 0 ] or die $!;
print 'Before indexing: ', time;
my $index = pack 'J', 0;
$index .= pack 'J', tell FILE while <FILE>;
print 'After indexing: ', time;
print 'Size of index: ', length $index;
for my $i ( map{ int rand( length( $index )/4 ) } 1 .. 10_000 ) {
my $line = unpack( 'J', substr $index, $i*4, 4 );
seek FILE, $line, 0;
chomp( $_ = <FILE> );
printf "\r$line : '%s'", $_;
}
print "\nAfter reading 10,000 random lines: ", time;
__END__
P:\test>433953 data\1millionlines.dat
Before indexing: 1109148435
After indexing: 1109148440
Size of index: 4000004
1087640 : '00108765'
After reading 10,000 random lines: 1109148441
Almost quick enough that you could avoid dropping to C++ :)
Win32 also supports Memory Mapped Files natively, complete with Copy-on-Write where applicable. I also think that tye's Win32API::File may give you access to some, if not all the apis required to use them. I'm not sure that it would work out any quicker than indexing though and the fact that the file is being written to may cause problems.
Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
|