in reply to Re: Risque Romantic Rosetta Roman Race
in thread Risque Romantic Rosetta Roman Race
The regex variant tested slightly slower on my machine, compared to the array-unpack-reduce demonstration.
# rtoa-pgatram-mce.pl # Example run: perl rtoa-pgatram-mce.pl t1.txt >mce.tmp # # Convert a "modern" Roman Numeral to its arabic (decimal) equivalent. # The alpabetic input string may be assumed to always contain a valid +Roman Numeral in the range 1-3999. # Roman numerals may be upper or lower case. # Error handling is not required. # For example: # input "XLII" should produce the arabic (decimal) value 42 # input "mi" should produce the arabic (decimal) value 1001 use strict; use warnings; use List::Util qw(sum); use Time::HiRes qw(time); use MCE; # Function roman_to_arabic # Output a list of their arabic (decimal) values to standard output. # sub roman_to_arabic { my $files = shift; # in: reference to a list of files containin +g Roman Numerals (one per line) my $apply_cpu_affinity = ( $^O =~ /linux/i ) ? 1 : 0; my %r2d; @r2d{qw( i ii iii iv v vi vii viii ix x xx xxx xl l lx lxx lxxx xc c cc ccc cd d dc dcc dccc cm m mm mmm )} = map { my $n = $_; map $n * $_, 1 .. 9 } 1, 10, 100, 1000; my $re = qr/ ix|iv|iii|ii|i| viii|vii|vi|v| xxx|xx|xl|xc|x| lxxx|lxx|lx|l| cm|cd|ccc|cc|c| dccc|dcc|dc|d| mmm|mm|m| ./x; # construct a pool of workers my $mce = MCE->new( max_workers => MCE::Util::get_ncpu(), chunk_size => 90 * 1024, init_relay => 1, # if defined, tells MCE to load MCE::Relay p +lus extra setup posix_exit => 1, use_slurpio => 1, user_begin => sub { if ( $apply_cpu_affinity ) { my $cpu = (MCE->wid - 1) % MCE::Util::get_ncpu(); system("taskset -cp $cpu $$ >/dev/null"); $apply_cpu_affinity = 0; } }, user_func => sub { my ( $mce, $slurp_ref, $chunk_id, $output ) = ( @_, '' ); open my $fh, '<', $slurp_ref; while ( <$fh> ) { chomp; $output .= sum @r2d{ (lc $_) =~ /$re/go }; $output .= "\n"; } close $fh; # output orderly MCE::relay { print $output }; } ); # process a list of files for my $fname ( @{$files} ) { warn("$0: cannot access '$fname': No such file or directory\n"), + next unless -e $fname; warn("$0: cannot access '$fname': Permission denied\n"), next unless -r $fname; warn("$0: cannot process '$fname': Is a directory\n"), next if -d $fname; $mce->process({ input_data => $fname }); } # reap MCE workers $mce->shutdown; } @ARGV or die "usage: $0 file...\n"; my @rtoa_files = @ARGV; warn "rtoa pgatram start\n"; my $tstart = time; roman_to_arabic(\@rtoa_files); warn sprintf("time %0.03f secs\n", time - $tstart);
Running with one worker.
# max_workers => 1 time reduce : 4.581 secs time regex : 4.658 secs
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Risque Romantic Rosetta Roman Race
by tybalt89 (Monsignor) on May 13, 2023 at 14:48 UTC | |
by marioroy (Prior) on May 13, 2023 at 16:24 UTC |