Jerry Hedden wrote:
> 1. Speed - retrieving the TID is faster than generating a
> 'threads=SCALAR(0x100110ec)' string.
>
> I did the tests:  This is a 5X (i.e. 400%) speed improvement!!!
>
> 2. More speed - the shorter the hash key the faster the
> lookup.  (Yes, I've tested this.  The more characters in the
> key, the more cycles it takes to compute the hash for that
> key.)
>
> And this is a 30% speed improvement!

> > Anyway, why are you using overloading if you care about
> > speed? I never bench marked it myself to see the
> > difference but I've gotten the impression that just having
> > this present in the class makes doing anything with the
> > object slower. For sure I've noticed a whole heap of
> > additional ISA walking when I've instrumented that on
> > overloverloaded objects.
>
> Unlike you, I did do the tests, and the overloading only
> costs 7% which is in the grass compared with the other
> improvements reported above.
>
> The bottom line in that $hash{$thr} and "$thr" are both more
> than 5 times faster with ""-overloading than without it.

Rick Delaney snidely replied:
> I can see no reason why anyone would believe these numbers
> without seeing the tests.

Gawd, I love being called a liar first thing in the morning.
It really gets my blood pumping.

First, let's see just how slow generating a 'default' object
string is.  (I'm going to call this a CTA string because it's
composed of the Class name, the Type and the Address).

The test below is a bit of an improvement over my original,
and shows that retrieving the TID is actually 6 times faster
than generating a CTA string:

    use strict;
    use warnings;
    no warnings 'void';

    use Benchmark qw(cmpthese);

    use threads;

    my $thr = threads->create(sub {});

    cmpthese( -1, {
        'str' => sub { ''.$thr      for (1..1000) },
        'tid' => sub { ''.$thr->tid for (1..1000) }
    });

    $thr->join();

Results:
          Rate  str  tid
    str 56.5/s   -- -83%
    tid  340/s 502%   --

Here is the test that justifies my statement that
""-overloading only costs 7%:

    use strict;
    use warnings;
    no warnings 'void';

    use Benchmark qw(cmpthese);

    use threads;
    {
        package threads;
        use overload ('""' => \&tid);
    }

    my $thr = threads->create(sub {});

    cmpthese( -1, {
        'str' => sub { ''.$thr      for (1..1000) },
        'tid' => sub { ''.$thr->tid for (1..1000) }
    });

    $thr->join();

Results:
         Rate str tid
    str 323/s  -- -7%
    tid 347/s  7%  --

And finally, the cost of using a CTA string for a hash key
verses using the TID for a hash key.  Again, this is an
improvement over my original which shows even better
results:

    use strict;
    use warnings;
    no warnings 'void';

    use Benchmark qw(cmpthese);

    use threads;

    my $thr = threads->create(sub {});

    my $str = overload::StrVal($thr);
    my $tid = $thr->tid();

    my %x = (
        $str => 1,
        $tid => 1
    );

    cmpthese( -1, {
        'str'  => sub { $x{$str} for (1..1000) },
        'tid'  => sub { $x{$tid} for (1..1000) }
    });

    $thr->join();

Results:
          Rate  str  tid
    str 2217/s   -- -38%
    tid 3581/s  62%   --

Now, let's put some of these together by using these tests
in the above:

    cmpthese( -1, {
        'str'  => sub { $x{$thr}      for (1..1000) },
        'tid'  => sub { $x{$thr->tid} for (1..1000) }
    });

Without ""-overloading:

          Rate  str  tid
    str 54.9/s   -- -85%
    tid  355/s 545%   --

With ""-overloading:

         Rate str tid
    str 329/s  -- -7%
    tid 353/s  7%  --

(And remember, folks, YMMV, so don't go quibbling to me with
statements of "well, I ran the tests and only got X".  The
bottom line is I did do the tests, and here's my results to
back up my statements.)

Rick Delaney also uttered:
> Not that the speed argument is valid anyway.

For the individual ops of "... $thr ..." and $hash{$thr},
the 6X performance improvement using ""-overloading of the
threads object is nothing to sneeze at.  Now, would this
significantly impact the performance of any particular "real
world" threaded application?  Probably not.  However, I
presented the performance improvements as PART of the reason
for ""-overloading of threads objects - not the whole
reason.  For me, the usability aspects are more important,
but the speed improvements are nice to have, too.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to