i’d like to know what exactly happens when i use reflection to call a method whose name i have as a string:
my $foo = Foo->new();
my $method = 'myMethod';
$foo->$method();
is ~20% slower than native call:
$foo->myMethod();
Any pointers to documentation about how perl’s reflection is implemented would be helpful.
Thanks.
First, I don’t trust benchmarks I don’t see. It’s too easy to get them wrong. I benchmarked them myself.
I can replicate your results.
That does seem rather big, but percentages can be very misleading with something so fast. Let’s look at the difference in absolute times.
Not that much. So where is that time spent?
It seems the only difference is an extra symbol table lookup. 100ns seems excessive for that. But to be sure, compare to something tiny, say like adding one.
Plugging that code and
our $x = 100;into the benchmark code above, we getSo,
So is it reasonable for a simple symbol table lookup to take twice as long as adding one? Probably, since it involves hashing a string and looking for a string in short linked list.
Do you really care about spending an extra 100ns here and there? No, I’m guessing.