Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Sum to 100 at Rosetta Code

by trippledubs (Deacon)
on Feb 06, 2019 at 16:53 UTC ( #1229484=note: print w/replies, xml ) Need Help??


in reply to Sum to 100 at Rosetta Code

Contrived from original problem, concurrent version. You have to increase the size of the kernel queue for it to work. Command is in script comment. If you don't, should exit cleanly, but get some error spam.
#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use IPC::SysV qw/IPC_PRIVATE S_IRUSR S_IWUSR IPC_NOWAIT/; use IPC::Msg; # echo $((2**31-1)) > /proc/sys/kernel/msgmnb my $q = IPC::Msg->new(IPC_PRIVATE,S_IRUSR|S_IWUSR); sub quit { $q->remove; } $SIG{INT} = \&quit; my $string = '12345678912345'; $q->snd(1,$string) or die "$!"; $q->snd(1,"-$string") or die "$!"; my $parent = $$; my $n_workers = shift // die; die "128 workers max" if ($n_workers > 7); fork // die $! for (1..$n_workers); while ( $q->rcv(local $_,32,0,IPC_NOWAIT) ) { say if eval == 100; while (/\d+?/g) { next if ( !$' || index( $', '-' ) > -1 || index( $', '+' ) > - +1 ); $q->snd(1,"$`$&+$'",IPC_NOWAIT) or die $!; $q->snd(1,"$`$&-$'",IPC_NOWAIT) or die $!; } } END { 1 until wait == -1; if ($$ == $parent) { quit; } }
  • 2x - 2m11s
  • 4x - 55s
  • 8x - 23s
  • 16x - 12s
  • 32x - 7s

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1229484]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-10-20 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (209 votes). Check out past polls.

    Notices?