From 22fb48cb473b6ae376b6ed91431732ea5639ac4f Mon Sep 17 00:00:00 2001 From: drbaggy Date: Wed, 28 Apr 2021 13:59:37 +0100 Subject: pushing new version of ch-2.pl with better notes and white space --- challenge-110/james-smith/perl/ch-2.pl | 52 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/challenge-110/james-smith/perl/ch-2.pl b/challenge-110/james-smith/perl/ch-2.pl index e02c2735a3..8ee3bb6690 100644 --- a/challenge-110/james-smith/perl/ch-2.pl +++ b/challenge-110/james-smith/perl/ch-2.pl @@ -37,19 +37,19 @@ select(STDOUT); $| = 1; my $t0; - $t0 = time; transpose_seek( $FN_TINY, 'seek-small' ); say 'Seek small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_regex( $FN_TINY, 'regex-small' ); say 'Regex small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_split( $FN_TINY, 'split-small' ); say 'Split small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_seek( $FN_SMALL, 'seek-1000' ); say 'Seek 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_seek( $FN_MEDIUM, 'seek-2000' ); say 'Seek 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_seek( $FN_LARGE, 'seek-5000' ); say 'Seek 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_regex( $FN_SMALL, 'regex-1000' ); say 'Regex 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_seek( $FN_MASSIVE, 'seek-30000' ); say 'Seek 30000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_regex( $FN_MEDIUM, 'regex-2000' ); say 'Regex 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_split( $FN_SMALL, 'split-1000' ); say 'Split 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_regex( $FN_LARGE, 'regex-5000' ); say 'Regex 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_split( $FN_MEDIUM, 'split-2000' ); say 'Split 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); - $t0 = time; transpose_split( $FN_LARGE, 'split-5000' ); say 'Split 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); +if($ARGV[0] == 1) { $t0 = time; transpose_seek( $FN_TINY, 'seek-small' ); say 'Seek small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 2) { $t0 = time; transpose_regex( $FN_TINY, 'regex-small' ); say 'Regex small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 3) { $t0 = time; transpose_split( $FN_TINY, 'split-small' ); say 'Split small - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 4) { $t0 = time; transpose_seek( $FN_SMALL, 'seek-1000' ); say 'Seek 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 5) { $t0 = time; transpose_seek( $FN_MEDIUM, 'seek-2000' ); say 'Seek 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 6) { $t0 = time; transpose_seek( $FN_LARGE, 'seek-5000' ); say 'Seek 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 7) { $t0 = time; transpose_regex( $FN_SMALL, 'regex-1000' ); say 'Regex 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 8) { $t0 = time; transpose_seek( $FN_MASSIVE, 'seek-30000' ); say 'Seek 30000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 9) { $t0 = time; transpose_regex( $FN_MEDIUM, 'regex-2000' ); say 'Regex 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 10) { $t0 = time; transpose_split( $FN_SMALL, 'split-1000' ); say 'Split 1000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 11) { $t0 = time; transpose_regex( $FN_LARGE, 'regex-5000' ); say 'Regex 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 12) { $t0 = time; transpose_split( $FN_MEDIUM, 'split-2000' ); say 'Split 2000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } +if($ARGV[0] == 13) { $t0 = time; transpose_split( $FN_LARGE, 'split-5000' ); say 'Split 5000 - Time: ',sprintf('%13.6f',time-$t0),' ',get_statm_info(); } sub transpose_split { ## Slurp into array @@ -74,36 +74,38 @@ sub transpose_regex { } sub transpose_seek { - my($prev,@pos) = (0); - - open my $fh, '<', $_[0]; - open my $ofh, '>', $_[1]; + my ( $prev, @pos ) = ( 0 ); ## Loop through the file and get the start/end position of each line, ## and the first $BYTES characters of each line... - ( push @pos, [ $prev+$BYTES, tell $fh, substr $_, 0, $BYTES] ) && + open my $fh, '<', $_[0]; + + ( push @pos, [ $prev+$BYTES, tell $fh, substr $_, 0, $BYTES ] ) && ( $prev = tell $fh ) while <$fh>; ## While we still have "columns" loop through each row and grab the first ## entry and output results. + open my $ofh, '>', $_[1]; ## Need 2 file handles open at once for this. + while( $pos[0][0] < $pos[0][1] || length $pos[0][2] ) { - my $j=''; - foreach(@pos) { + my $j = ''; + foreach( @pos ) { ## Grab extra data for the row if we have got an incomplete ## field {missing a "," and still data to read} while( $_->[2] !~ m{,} && $_->[0] < $_->[1] ) { - seek $fh, $_->[0], 0; - read $fh, $_->[2], ## "Buffer" + seek $fh, $_->[0], 0; ## 0 = from start of file! + read $fh, + $_->[2], ## "Buffer" $_->[1]-$_->[0] > $BYTES ? $BYTES : $_->[1]-$_->[0], length $_->[2]; ## Length of "Buffer" so text gets ## added to end - $_->[0]+=$BYTES; + $_->[0] += $BYTES; } $_->[2] =~ s{^([^,\r\n]+)[,\r\n]*}{}; - print {$ofh} $j,$1; - $j||=','; + print {$ofh} $j, $1; + $j ||= ','; } say {$ofh} ''; } -- cgit