#!perl -w use strict; =head1 NAME testio.pl =head1 DESCRIPTION test io impact of reading lines (sip), reading in an array (array), or reading in the entire file (slurp). The array reads the entire file into memory into an array. The slurp reads the entire file into memory in a long string. The sip reads a line at a time and writes it back. Bio applications often need to search for patterns in one continuous string per sequence. =head1 SYNOPSIS testio.pl =head1 NOTES Create bigfile.txt to create a benchmark. It must be large enough to be measured in seconds. On a slow, slow PC the timing of a 21 MB file (21,000,000 characters) was: Sip time 4 seconds Array time 11 seconds Slurp time 3 seconds =head1 AUTHOR David Scott from ideas on Steve Litt's Perls of Wisdom at www.troubleshooters.com =cut my $bigfileName = "bigfile.txt"; my $sipfileName = "sip.out"; my $arrayfileName = "array.out"; my $slurpfileName = "slurp.out"; package main; { my $start = time(); print "Starting sip\n"; sip(); print "End sip\n"; my $end_sip = time(); print "Starting array\n"; buildarray(); print "End array\n"; my $end_array = time(); print "Starting slurp\n"; slurp(); print "End slurp\n"; my $end_slurp = time(); print "Sip time is ", $end_sip-$start, " seconds\n"; print "Array time is ", $end_array-$end_sip, " seconds\n"; print "Slurp time is ", $end_slurp-$end_array, " seconds\n"; } # functions # read and write file in one fell slurp sub slurp { my $in_file; my $out_file; local $/; open ($in_file, "< $bigfileName") or die("Error reading $bigfileName for benchmark: $!"); open $out_file, ">" . $slurpfileName; my $buf = <$in_file>; print $out_file $buf; close $out_file; close $in_file; } # read and write file one line at a time sub sip { my $in_file; my $out_file; open($in_file, "< $bigfileName") or die("Create $bigfileName for benchmark"); open $out_file, ">" . $sipfileName; while(<$in_file>) { my $line = $_; # chomp $line; # print $out_file $line, "\n"; print $out_file $line; } close $out_file; close $in_file; } # read and write the file as an array of lines sub buildarray { my $in_file; my $out_file; my @array; open($in_file, "< $bigfileName") or die("Create $bigfileName for benchmark"); open $out_file, ">" . $arrayfileName; @array = <$in_file>; print $out_file @array; close $out_file; close $in_file; }