Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6888847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:04:47+00:00 2026-05-27T06:04:47+00:00

i’d like to know what exactly happens when i use reflection to call a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:04:48+00:00Added an answer on May 27, 2026 at 6:04 am

    First, I don’t trust benchmarks I don’t see. It’s too easy to get them wrong. I benchmarked them myself.

    use strict;
    use warnings;
    
    use Benchmark qw( cmpthese );
    
    sub new { return bless({}, $_[0]); }
    sub myMethod { }
    
    my %tests = (
       rt => '$foo->$method()  for 1..1000;',
       ct => '$foo->myMethod() for 1..1000;',
    );
    
    $_ = 'use strict; use warnings; our $foo; our $method; ' . $_
       for values(%tests);
    
    our $foo = __PACKAGE__->new();
    our $method = 'myMethod';
    
    cmpthese(-3, \%tests);
    

    I can replicate your results.

         Rate   rt   ct
    rt 1879/s   -- -19%
    ct 2333/s  24%   --
    
    (Rate is 1/1000th of actual rate.)
    

    That does seem rather big, but percentages can be very misleading with something so fast. Let’s look at the difference in absolute times.

    Compile-time: 2333000 calls per second = 429 nanoseconds per call
    Run-time:     1879000 calls per second = 532 nanoseconds per call
    Difference:   103 nanoseconds per call.
    

    Not that much. So where is that time spent?

    $ perl -MO=Concise,-exec -e'$foo->myMethod()'     $ perl -MO=Concise,-exec -e'$foo->$method()'
    1  <0> enter                                   =  1  <0> enter 
    2  <;> nextstate(main 1 -e:1) v:{              =  2  <;> nextstate(main 1 -e:1) v:{
    3  <0> pushmark s                              =  3  <0> pushmark s
    4  <#> gvsv[*foo] s                            =  4  <#> gvsv[*foo] s
                                                   +  5  <#> gvsv[*method] s
    5  <$> method_named[PV "myMethod"]             !  6  <1> method K/1
    6  <1> entersub[t2] vKS/TARG                   =  7  <1> entersub[t3] vKS/TARG
    7  <@> leave[1 ref] vKP/REFC                   =  8  <@> leave[1 ref] vKP/REFC
    -e syntax OK                                   =  -e syntax OK
    

    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.

    $ perl -MO=Concise,-exec -e'my $y = $x;'     $ perl -MO=Concise,-exec -e'my $y = $x + 1;'
    1  <0> enter                              =  1  <0> enter 
    2  <;> nextstate(main 1 -e:1) v:{         =  2  <;> nextstate(main 1 -e:1) v:{
    3  <#> gvsv[*x] s                         =  3  <#> gvsv[*x] s
                                              +  4  <$> const[IV 1] s
                                              +  5  <2> add[t3] sK/2
    4  <0> padsv[$y:1,2] sRM*/LVINTRO         =  6  <0> padsv[$y:1,2] sRM*/LVINTRO
    5  <2> sassign vKS/2                      =  7  <2> sassign vKS/2
    6  <@> leave[1 ref] vKP/REFC              =  8  <@> leave[1 ref] vKP/REFC
    -e syntax OK                              =  -e syntax OK
    

    Plugging that code and our $x = 100; into the benchmark code above, we get

                Rate addition  baseline
    addition  4839/s       --      -26%
    baseline  6532/s      35%        --
    
    (Rate is 1/1000th of actual rate.)
    

    So,

    Basline:    6553000/s = 153 nanoseconds per assignment
    Addition:   4839000/s = 207 nanoseconds per assignment+addition
    Difference:              54 nanoseconds per addition
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.