My problem is this: I want to test membership in a small set, many thousands of times. The set is a constant and I want my program to enforce that fact -- e.g. I don't want to use a global variable, unless I can guarantee that it is constant.
But, I don't have the ability to install and use Readonly::XS on my system.
My first try:
sub IsMember { my %Set = ( 'age' => 1, 'old' => 1, 'years' => 1); return $Set{$_[0]}; }
This seems to work okay, but profiling shows a significant amount of time in this subfunction (about 11 percent of my total run time.)
I also tried the same thing with a closure, just in case the %Set was being created on the stack each time:
I also tried a regex, but still no significant speedup:{ my %Set = ( 'age' => 1, 'old' => 1, 'years' => 1); sub IsMember { return $Set{$_[0]}; } }
I want to use a global constant hash and then access it directly (instead of through a function wrapper). But I cannot figure this out. I have perl 5.6.1 (and again, Readonly:XS is not an option at this time.) If no speedup can be expected - any suggestions on the best implementation of a constant hash (given my constraints) would be enormously helpful. - Studentsub IsMember { return $_[0] =~ m/age|old|years/; }
In reply to Fastest way to test membership in a constant set by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |