------------------------------------ "ARRAY" BEST TIME : 1.227776 "ARRAY" TOTAL TIME: 6.73529 "WHILE" BEST TIME : 1.103754 "WHILE" TOTAL TIME: 5.71099 ------------------------------------ #### #!/usr/bin/perl -w ########################################## # # # PURE PERL FILE WRITE BENCHMARK # # METHODS TESTED: ARRAY vs WHILE # # # ########################################## use strict; my $TEST_FILE_SIZE_MB = 100; my $PASSES = 5; eval('use Time::HiRes;'); if ($@) { error('Couldn\'t load required libraries.'); } my $file = "./test.txt"; my $tempfile = $file.'.tmp'.int(rand()*99999); my $flagfile = $file.'.lock'; my $log; &testfilecheck; my $debug; my ($best_time_array,$best_time_while, $total_time_array, $total_time_while); for (my $x=0; $x < $PASSES; $x++){ my ($result,$dbg) = use_while(); $total_time_while+=$result; $best_time_while = $result if ($best_time_while > $result || !$best_time_while); $debug.=$dbg."\n"; } sleep 1; for (my $x=0; $x < $PASSES; $x++){ my ($result,$dbg) = use_array(); $total_time_array+=$result; $best_time_array = $result if ($best_time_array > $result || !$best_time_array); $debug.=$dbg."\n"; } print "Content-type: text/plain\n\n"; print <$file"); for (my $i=0; $i < $TEST_FILE_SIZE_MB*1000; $i++){ my $rnd; for (my $y=0; $y < 988; $y++){ $rnd.=int(rand()*9); } print NEW $rnd.'|'.time()."\n"; } close NEW; } } sub use_array{ my $startexectimemilliseconds = [ Time::HiRes::gettimeofday( ) ]; my ($debug,$count,$lastline); open (DAT, "+<$file"); flock DAT, 2; my @DATfile=; seek (DAT, 0, 0); truncate (DAT,0); foreach my $line(@DATfile){ chomp ($line); my $replace = '|'.time(); $line=~s/\|\d+$/$replace/; print DAT $line."\n"; $lastline = $line; $count++; } close DAT; my $elapsedtime = Time::HiRes::tv_interval( $startexectimemilliseconds ); $debug=<$flagfile"); flock LOCK, 2; open (DAT, $file); flock DAT, 2; open (TMP, ">$tempfile"); flock TMP, 2; while (my $line = ){ chomp ($line); my $replace = '|'.time(); $line=~s/\|\d+$/$replace/; print TMP $line."\n"; $lastline = $line; $count++; } close TMP; close DAT; rename($tempfile,"$file"); close LOCK; my $elapsedtime = Time::HiRes::tv_interval( $startexectimemilliseconds ); $debug=<