develooper Front page | perl.perl5.porters | Postings from January 2022

Re: PSC #049 2022-01-07

Thread Previous
From:
Scott Baker
Date:
January 12, 2022 21:28
Subject:
Re: PSC #049 2022-01-07
Message ID:
cb580ecf-a814-fcf2-f300-546ba83ca7e3@perturb.org
I took some time and put rjbs's base script in to a |benchmark| 
framework. I added two functions to simplify extracting |$x| instead of 
using |shift()|. The overhead of using an array for this seems to take a 
toll. After simplifying it |base_simp| is the fastest, but |sigs| is 
very close behind.

|#!/usr/bin/env perl use strict; use warnings; use Benchmark 
qw(cmpthese); use experimental 'signatures'; 
############################################################################### 
my $count = 10000000; cmpthese($count, { 'sigs' => sub { sigs(10) ; }, 
'bare' => sub { bare(10) ; }, 'bare_simp' => sub { bare_simp(10); }, 
'full' => sub { full(10) ; }, 'full_simp' => sub { full_simp(10); }, }); 
sub sigs ($x) { return $x * $x } sub bare { my ($x) = @_; return $x * $x 
} sub bare_simp { my $x = $_[0]; return $x * $x } sub full { die "arity" 
unless @_ == 1; my ($x) = @_; return $x * $x } sub full_simp { die 
"arity" unless @_ == 1; my $x = $_[0]; return $x * $x } __END__ $ time 
perl /tmp/x.pl Rate full_simp full bare sigs bare_simp full_simp 
5263158/s -- -6% -21% -29% -33% full 5617978/s 7% -- -16% -24% -28% bare 
6666667/s 27% 19% -- -10% -15% sigs 7407407/s 41% 32% 11% -- -5% 
bare_simp 7812500/s 48% 39% 17% 5% -- real 0m19.556s user 0m19.550s sys 
0m0.003s |

On 1/12/2022 1:06 PM, Alexander Hartmaier wrote:

> Wow, that's amazing! I wasn't aware that sub sigs are faster and also 
> by that much!
>
> Here are the times from my PC (Ryzen 3800X):
> sigs: 0.7614s
> bare: 0.8764s
> full: 1.0507s
>
> The multiplication affects the difference even more, when replaced 
> with just 'return $x' these are the results:
> sigs: 0.6784s
> bare: 0.8124s
> full: 0.9844s
>
> I've tried to convert your script to use Benchmark cmpthese but failed.

​
Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About