sub make_accessor { my $self = shift; my ($name, $index) = @_; no strict 'refs'; *{ref($self) . "::$name"} = sub { my $self = shift; return $self->[$index] unless @_; $self->[$index] = $_[0]; $self; }; 1; }
That's a lot of extra lexicals that aren't being used in the closure. Does it carry them around? I rewrote it to look something like:
sub make_accessor { my ($index) = $_[2]; no strict 'refs'; *{ref($_[0]) . "::$_[1]"} = sub { return $_[0]->[$index] unless @_ > 1; $_[0]->[$index] = $_[1]; $_[0]; }; 1; }
I benchmarked as follows and had the following results:
use Benchmark qw(cmpthese); my @array; make_accessor1(code1, 0); make_accessor2(code2, 0); cmpthese(-10, { lexicals => sub { code1(5) }, no_lexic => sub { code2(5) }, }); sub make_accessor1 { my ($name, $index) = @_; *{$name} = sub { $array[$index] = $_[0] }; } sub make_accessor2 { my $index = $_[1]; *{$_[0]} = sub { $array[$index] = $_[0] }; }
Benchmark: running lexicals, no_lexic, each for at least 5 CPU seconds +... lexicals: 5 wallclock secs ( 5.43 usr + -0.01 sys = 5.42 CPU) @ 70 +0151.48/s (n=3794821) no_lexic: 7 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 71 +6661.80/s (n=3826974) Rate lexicals no_lexic lexicals 700151/s -- -2% no_lexic 716662/s 2% --
The speed difference wasn't impressive. But, I don't know how to check memory usage ... (Obviously, in the case above, it won't be large, but if the closure creation was more complicated ... ?)
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
In reply to Closures and lexical scope by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |