Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: "require Carp" may be hazardous to your code (assume)

by ikegami (Patriarch)
on Dec 05, 2006 at 19:20 UTC ( [id://587955]=note: print w/replies, xml ) Need Help??


in reply to Re^3: "require Carp" may be hazardous to your code (assume)
in thread "require Carp" may be hazardous to your code

Note that I kick quite a kick out of the last half of that statement. I'm sure the very first thing that require does is exactly that anyway, so I'd never add such trivial code in an obvious attempt at micro-optimization.

Did you read the comment next to "require Carp::Heavy unless $INC{"Carp/Heavy.pm"};" in the source? It's not an optimization. It's done that way so the module runs when Safe disallows require.

Update:

I don't recall you having posted data to support your claim either.

( On average, it takes me 4.24 ns to load Carp. Oops, Time::HiRes loads Carp. New benchmarks in progress. )

>perl -MTime::HiRes=time -le "$t1=time; require Carp; $t2=time; print +$t2-$t1" 4.05311584472656e-006 >perl -MTime::HiRes=time -le "$t1=time; require Carp; $t2=time; print +$t2-$t1" 4.05311584472656e-006 >perl -MTime::HiRes=time -le "$t1=time; require Carp; $t2=time; print +$t2-$t1" 4.05311584472656e-006 >perl -MTime::HiRes=time -le "$t1=time; require Carp; $t2=time; print +$t2-$t1" 5.00679016113281e-006 >perl -MTime::HiRes=time -le "$t1=time; require Carp; $t2=time; print +$t2-$t1" 4.05311584472656e-006

Replies are listed 'Best First'.
Re^5: "require Carp" may be hazardous to your code (assume)
by tye (Sage) on Dec 05, 2006 at 20:02 UTC

    Congratulations on your broken benchmark:

    > perl -MTime::HiRes -le"print $INC{'Carp.pm'}" .../Carp.pm

    Note that you also didn't address that this practice predates the factorying out of the "heavy" parts.

    - tye        

      Nice catch. I thought it was a bit fast...

      Without heavy bits: 1.34 ms

      >perl -le "$t1=time; for (1..$ARGV[0]) { delete $INC{'Carp.pm'}; requi +re Carp; } $t2=time; print(($t2-$t1)/$ARGV[0])" 100000 0.00134 >perl -le "$t1=time; for (1..$ARGV[0]) { delete $INC{'Carp.pm'}; requi +re Carp; } $t2=time; print(($t2-$t1)/$ARGV[0])" 100000 0.00134 >perl -le "$t1=time; for (1..$ARGV[0]) { delete $INC{'Carp.pm'}; requi +re Carp; } $t2=time; print(($t2-$t1)/$ARGV[0])" 100000 0.00134

      With heavy bits: 4.75 ms

      >perl -le "$t1=time; for (1..$ARGV[0]) { delete $INC{'Carp.pm'}; delet +e $INC{'Carp/Heavy.pm'}; require Carp; require Carp::Heavy; } $t2=tim +e; print(($t2-$t1)/$ARGV[0])" 100000 0.00475 >perl -le "$t1=time; for (1..$ARGV[0]) { delete $INC{'Carp.pm'}; delet +e $INC{'Carp/Heavy.pm'}; require Carp; require Carp::Heavy; } $t2=tim +e; print(($t2-$t1)/$ARGV[0])" 100000 0.00474
Re^5: "require Carp" may be hazardous to your code (assume)
by grinder (Bishop) on Dec 05, 2006 at 20:48 UTC

    Using much more lightweight code (for a FreeBSD system), I get around 30 milliseconds when not loading Carp, and 90 msecs when I do. Here is the code I used:

    use Carp; # comment this line out, or not use BSD::Process; my $t = BSD::Process->new->elapsed_time; print $t, $/; # time in milliseconds

    The times are very erratic. It seems to boil down to whether the process executes within one timeslice of the processor or not: there are definite quanta to the amount of time taken. Nevertheless, sometimes people may care about 0.06 seconds of startup time (certainly less non-negligible than 4 usec).

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found