in reply to Re: Is there any (performance) benefit to importing functions?
in thread Is there any (performance) benefit to importing functions?

However, if you do bench it, please do post results.

#!/usr/bin/perl use warnings; use strict; { package Exported; use Time::HiRes qw{ gettimeofday }; sub foo { gettimeofday } } { package FullName; use Time::HiRes; sub foo { Time::HiRes::gettimeofday } } { package CodeRef; use Time::HiRes; *foo = *Time::HiRes::gettimeofday{CODE}; } use Test::More tests => 2; cmp_ok abs(Exported::foo() - FullName::foo()), '<', 1e-4, 'almost same +'; cmp_ok abs(Exported::foo() - CodeRef::foo()), '<', 1e-4, 'almost same +'; use Benchmark qw{ cmpthese }; cmpthese(-3, { exported => \&Exported::foo, fullname => \&FullName::foo, coderef => \&CodeRef::foo, });

On my i3 notebook:
Rate exportedfullnamecoderef
exported3537621/s -- -6% -72%
fullname3753422/s 6% -- -70%
coderef 12498685/s253% 233% --

Update

Thanks haukex, the coderef benchmark is wrong. Adding a sub around it slows it to a comparable level in 5.18.2 (blead still shows coderef about 10% faster):
coderef => sub { CodeRef::foo() },
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^3: Is there any (performance) benefit to importing functions?
by stevieb (Canon) on Feb 10, 2018 at 22:26 UTC

    Thanks choroba!

    So export and fullname are negligibly different, but holy crap coderef! :)

      but holy crap coderef!

      That case isn't really equivalent to the others - the exported and fullname cases have an extra sub { } wrapped around them.

      use warnings; use strict; use Benchmark qw/cmpthese/; sub foo { 1+2==3 } cmpthese(1e8, { one => \&foo, two => sub { foo() }, }); __END__ Rate two one two 24271845/s -- -72% one 86956522/s 258% --