Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

While using forks::shared I've noticed that access to shared variables is incredibly slow. Consider the following example:
use strict; use forks; use forks::shared; my $t = time; my %hash : shared = (); share( %hash ); $hash{ $_ } = $_ % 2 for( 1..500 ); print "$_: $hash{ $_ }\n" for( sort { $a <=> $b } keys %hash ); print "Elapsed: " . (time - $t) . "\n"; exit 0;
On both systems I've tested on (Dual P4, Red Hat and Dual P3, Gentoo both using perl 5.8.3) this takes ~15 seconds to run. The same script without the shared variable runs in a fraction of a second. Example:
use strict; my $t = time; my %hash = (); $hash{ $_ } = $_ % 2 for( 1..500 ); print "$_: $hash{ $_ }\n" for( sort { $a <=> $b } keys %hash ); print "Elapsed: " . (time - $t) . "\n"; exit 0;
Anyone have experience using forks::shared who might be able to help me speed this up?

Replies are listed 'Best First'.
Re: forks::shared speed problem
by perrin (Chancellor) on May 03, 2004 at 19:31 UTC
    forks::shared is pretty cool, but it uses IPC and tied variables, both of which are relatively slow. I suspect you would have better performance with one of the fast IPC modules like IPC::MM, BerkeleyDB, or even DBI and MySQL, but they would require significant changes in your code. If you are concerned about speed, don't use the tied interface on any of these. You can always call methods like FETCH() directly instead.