I wrote a little benchmark test to check the performance of different implementations

package MyClass1;

sub new
{
  my $invocant = shift;
  my $class    = ref($invocant) || $invocant;
  my $self     = undef;


    #Set the Default Attributes and assign the initial Values
    $self = {
        "_report" => "",
        "_count" => 0
    };


    #Bestow Objecthood
    bless $self, $class;


    #Give the Object back
    return $self;
  
}

sub newFunctionName
{
  my $self = shift;
  
  $self->{"_count"} = shift;
  $self->{"_report"} = __PACKAGE__ . " - run no. '" . $self->{"_count"} . "'\n";
}

sub oldFunctionName
{
  my $self = shift;
  
  $self->newFunctionName(@_);
}


package MyClass2;

sub new
{
  my $invocant = shift;
  my $class    = ref($invocant) || $invocant;
  my $self     = undef;


    #Set the Default Attributes and assign the initial Values
    $self = {
        "_report" => "",
        "_count" => 0
    };


    #Bestow Objecthood
    bless $self, $class;


    #Give the Object back
    return $self;
  
}

sub newFunctionName
{
  my $self = shift;
  
  $self->{"_count"} = shift;
  $self->{"_report"} = __PACKAGE__ . " - run no. '" . $self->{"_count"} . "'\n";
}

sub oldFunctionName
{
  goto &newFunctionName;
}


package MyClass3;

sub new
{
  my $invocant = shift;
  my $class    = ref($invocant) || $invocant;
  my $self     = undef;


    #Set the Default Attributes and assign the initial Values
    $self = {
        "_report" => "",
        "_count" => 0
    };


    #Bestow Objecthood
    bless $self, $class;


    #Give the Object back
    return $self;
  
}

sub newFunctionName
{
  my $self = shift;
  
  $self->{"_count"} = shift;
  $self->{"_report"} = __PACKAGE__ . " - run no. '" . $self->{"_count"} . "'\n";
}

sub oldFunctionName
{
  &newFunctionName;
}


package main;

use Benchmark;

     
my $o1 = MyClass1->new;
my $o2 = MyClass2->new;
my $o3 = MyClass3->new;
my $icnt = 0;

     
     
print "count 0: '$icnt':\n";

print "o1 report 0 (count: '" . $o1->{"_count"} . "'): '" . $o1->{"_report"} . "'\n";
print "o2 report 0 (count: '" . $o2->{"_count"} . "'): '" . $o2->{"_report"} . "'\n";
print "o3 report 0 (count: '" . $o3->{"_count"} . "'): '" . $o3->{"_report"} . "'\n";

 timethese( 10000000, {
 'MyClass1'  => sub { 
 
 $icnt = 0 if($o1->{"_count"} == 0);
 
 $icnt++;
 $o1->oldFunctionName($icnt);
 

},
 'MyClass2'  => sub { 

 $icnt = 0 if($o2->{"_count"} == 0);
 
 $icnt++;
 $o2->oldFunctionName($icnt);


 },
 'MyClass3'  => sub { 

 $icnt = 0 if($o3->{"_count"} == 0);
 
 $icnt++;  
 $o3->oldFunctionName($icnt);


 },
 });

     
print "count 0: '$icnt':\n";

print "o1 report 0 (count: '" . $o1->{"_count"} . "'): '" . $o1->{"_report"} . "'\n";
print "o2 report 0 (count: '" . $o2->{"_count"} . "'): '" . $o2->{"_report"} . "'\n";
print "o3 report 0 (count: '" . $o3->{"_count"} . "'): '" . $o3->{"_report"} . "'\n";

The test result was:

count 0: '0':
o1 report 0 (count: '0'): ''
o2 report 0 (count: '0'): ''
o3 report 0 (count: '0'): ''
Benchmark: timing 10000000 iterations of MyClass1, MyClass2, MyClass3...
  MyClass1:  8 wallclock secs ( 7.48 usr +  0.00 sys =  7.48 CPU) @ 1336898.40/s (n=10000000)
  MyClass2:  7 wallclock secs ( 6.61 usr +  0.00 sys =  6.61 CPU) @ 1512859.30/s (n=10000000)
  MyClass3:  6 wallclock secs ( 5.96 usr +  0.00 sys =  5.96 CPU) @ 1677852.35/s (n=10000000)
count 0: '10000000':
o1 report 0 (count: '10000000'): 'MyClass1 - run no. '10000000'
'
o2 report 0 (count: '10000000'): 'MyClass2 - run no. '10000000'
'
o3 report 0 (count: '10000000'): 'MyClass3 - run no. '10000000'
'

I repeated the test several times and it gives always the same result pattern.
- The slowest performant implementation is MyClass1 with the $self->newFunctionName(@_); call
- The MyClass2::oldFunctionName() implementation with the goto statement performs already better.
- But the best performance is achieved with the MyClass3::oldFunctionName() implementation with the &newFunctionName; call

I didn't know this possible Syntax yet.
So thank you very much!


In reply to Re^5: Number of times I've used goto in Perl by HugoNo1
in thread Number of times I've used goto in Perl by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.