Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)


"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Tuesday April 09, 2002
04:51 AM


[ #4049 ]
Dear Log:

A thing I do a lot:

use constant nil => [];

foreach my $x (@{ $thing || nil }) {

Ideally it's reusing the same arrayref every time, whereas if I did "@{ $thing || [] }" it'd be making a new empty arrayref every time $thing is false, just to immediately throw it away. To wit:

use strict;
use constant nil => [];
use Benchmark;
my $y;
way1 => sub {foreach my $x (@{$y || nil}) { print ''; } return },
way2 => sub {foreach my $x (@{$y || []}) { print ''; } return },

Benchmark: timing 10000000 iterations of way1, way2...
way1: 22 wallclock secs (21.25 usr + 0.00 sys = 21.25 CPU) @ 470588.24/s (n=10000000)
way2: 47 wallclock secs (46.30 usr + 0.00 sys = 46.30 CPU) @ 215982.72/s (n=10000000)

So, a whopping ~3 MICROsecond difference -- compared to something probably astronomically greater just to load and call its import(). Ahwell, I can dream, can't I? And what if I think "|| nil" just looks better? IS THAT SO WRONG?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.