You can set the SvREADONLY
flag on a variable with Readonly::XS
, but that does not improve the efficiency. Efficiency comes from choosing the right algorithm, not through compiler hints. If you want your code to be faster/use less memory then profile it (see Devel::NYTProf
). When you find a bottleneck either use a different algorithm there or switch to use XS
.
Also, if you are going to try to optimize something, make sure the result really is faster, here is substr vs unpack:
Rate unpack substr
unpack 2055647/s -- -74%
substr 7989875/s 289% --
Here is the benchmark code.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my %subs = (
unpack => sub { return unpack "a3", "foobarbaz" },
substr => sub { return substr "foobarbaz", 0, 3 }
);
for my $sub (keys %subs) {
print "$sub => ", $subs{$sub}(), "\n";
}
Benchmark::cmpthese -1, \%subs;