in reply to Re: Efficiency Question
in thread Efficiency Question

I first had it coded as you suggested, but two thoughts occured to me:
  1. Keeping in inside an array would be faster to walk (foreach (@arr) has to be faster than doing 30 hash lookups).
  2. An order (and count) needed to be represented (for maintainence purposes) anyway, so why not just keep it in an array ref in the first place. I thought of the time stamp, but looking it up and testing for it would consume MUCH more time than a simple FILO algo. Remember, I'm trying my damnest to minimize the work the cache has to do.
Heck, I even tried blessing an array ref instead of a hash hoping I could make a class from that; but of course that didn't work :)

Now, here is an addition question. Is

$class->method();
any slower than
Class::method();
? And by "slower", I mean technically, not noticably :)

TIA!

Replies are listed 'Best First'.
Re: Re: Re: Efficiency Question
by clintp (Curate) on Feb 23, 2001 at 20:20 UTC
    The general wisdom says that yes, method calls are slower than function calls. (In some cases DRAMATICALLY slower.) For a simple case though, lets find out:
    use Benchmark; package Foo; sub bar { 1; } package main; timethese(1000000, { method => sub { Foo->bar(); }, func => sub { Foo::bar(); }, } );
    Drumroll....
    Benchmark: timing 1000000 iterations of func, method...
    func: 1 wallclock secs ( 1.93 usr + 0.00 sys = 1.93 CPU) @ 518134.72/s (n=1000000)
    method: 6 wallclock secs ( 4.95 usr + 0.00 sys = 4.95 CPU) @ 202020.20/s (n=1000000)
    Yup! When in doubt, Benchmark.
Re (tilly) 4: Efficiency Question
by tilly (Archbishop) on Feb 24, 2001 at 01:06 UTC
    Yes, 30 hash lookups is slower than walking an array with foreach.

    OTOH one hash lookup is much faster than walking a good chunk of an array.

    If you can arrange that straight lookups happen more often than having to walk the whole cache, then you should get a performance win. (See code in my other node.)

Re: (3) Efficiency Question
by MeowChow (Vicar) on Feb 24, 2001 at 00:51 UTC
    You should also consider that method invocation on a blessed reference is much faster than method invocation on class name.

    There is rather little performance overhead on object method calls. In addition, the two calls you suggest are not really an apples-apples comparison, since the class method passes the class name as a param, and the direct invocation does not (if factored in, the direct invocation becomes about 20% slower than shown below).

    package Foo; use strict; use warnings; use Benchmark qw(cmpthese); sub new { bless {}, shift; } sub meth { 1 } my $obj = new Foo; cmpthese (-3, { class => sub { Foo->meth($obj) }, method => sub { $obj->meth() }, direct => sub { Foo::meth() }, }); ### results Rate class method direct class 464126/s -- -50% -64% method 923914/s 99% -- -28% direct 1289819/s 178% 40% --
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print