Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

my versus our, why is my slower when script ends (global cleanup)

by marioroy (Priest)
on Apr 22, 2017 at 01:51 UTC ( #1188596=perlquestion: print w/replies, xml ) Need Help??
marioroy has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks,

On Mac OS X and Linux (not tested on other Unix platforms), there appears to be extra overhead by Perl prior to the script exiting for variables declared with my. The extra time is noticeable after seeing the "end" output.

Why does Perl have this odd behavior during cleanup?

use strict; use warnings; $| = 1; my $size = 2e6; my $data = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_CCCC', 0 ]; print "begin\n"; my %barcode_hash = map { $_ => $data } 1 .. $size; print "end\n";

There is practically no delay after seeing "end" if I declare the variable with our.

... our %barcode_hash = map { $_ => $data } 1 .. $size; ...

I made a script to capture the running time, helpful on Windows. Unix has time command and shell builtin time.

use strict; use warnings; use Time::HiRes 'time'; my $start_time = time; system(@ARGV) == 0 or die "\"@ARGV\" failed with error: $?"; printf "%0.03f seconds\n", time - $start_time;


perl perl my %big_hash: 2.726 seconds our %big_hash: 1.351 seconds

Thank you, karlgoethebier for this enlightenment. I do not understand what Perl is doing during cleanup. Has anyone encountered this behavior? I tested on Windows and is nothing like seen on Mac OS X and Linux.

Thanks, Mario

Replies are listed 'Best First'.
Re: my versus our, why is my slower when script ends (global cleanup)
by Anonymous Monk on Apr 22, 2017 at 02:19 UTC

      Thank you for this enlightenment. I'm learning Perl all the time. I don't know what should be happening, why there is a difference until reading your reply. I tested with require POSIX and POSIX::_exit(0) placed at the end of the script. The MCE and MCE::Hobo modules call kill 9 when posix_exit => 1 is given for minimum consumption so not having to load POSIX before spawning workers.

      use strict; use warnings; $| = 1; my $size = 2e6; my $data = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_CCCC', 0 ]; print "begin\n"; my %barcode_hash = map { $_ => $data } 1 .. $size; print "end\n"; require POSIX; POSIX::_exit(0);

      Calling POSIX::_exit(0) seems helpful for scripts making a big array or hash. Regarding MCE, it means having to call MCE::Loop->finish or $mce->shutdown explicitly so that socket handles and what not are released immediately and not linger around until the OS releases them. Regarding MCE::Shared, calling MCE::Shared->stop prior to exiting.

      Thank you, Anonymous_Monk. I never thought to call POSIX::_exit(0) from the main process. That works out well. I'm going to take the next hour or so and update the few examples in reply to karlgoethebier's post. Out to the rescue for this wonderful Monk++. All credits to you++ for explaining what's going on.

      Regards, Mario.

      Update: Oh my. I like having the destructors run in the event omitting calling MCE::Loop->finish or $mce->shutdown. It's nice that Perl gives us the option to declare a variable global when having to construct a very big array or hash and still have destructors run.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1188596]
Front-paged by stevieb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2018-03-22 18:55 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (283 votes). Check out past polls.