in reply to Ordering objects using external index
In the benchmark below, this resulted in a speedup of about 9x. Of course you do have to pay a little for maintaining the %uid2msg index but I'm assuming in your case you do a lot more reading than inserting and deleting.
Once you've done that, you can further speed things up with hash slice in the return, changing
toreturn [ map { $uid2msg{$_} } @$uids ];
This doesn't make much of difference in the original version but it more than doubles performance when %uid2msg uses a precomputed index.return [ @uid2msg{ @$uids } ];
Here's the results of a benchmark for 1000 msg objects
and 10000 msg objectsBenchmark: running hashslice, hashslice_pre, original, original_pre fo +r at least 5 CPU seconds... hashslice: 5 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 97 +.35/s (n=515) hashslice_pre: 5 wallclock secs ( 5.30 usr + 0.01 sys = 5.31 CPU) @ + 1757.63/s (n=9333) original: 5 wallclock secs ( 5.30 usr + 0.00 sys = 5.30 CPU) @ 91 +.51/s (n=485) original_pre: 5 wallclock secs ( 5.33 usr + 0.00 sys = 5.33 CPU) @ +817.07/s (n=4355)
the _pre versions are hugely faster. Code belowBenchmark: running hashslice, hashslice_pre, original, original_pre fo +r at least 5 CPU seconds... hashslice: 5 wallclock secs ( 5.04 usr + 0.04 sys = 5.08 CPU) @ 7 +.28/s (n=37) hashslice_pre: 5 wallclock secs ( 5.27 usr + 0.01 sys = 5.28 CPU) @ + 93.56/s (n=494) original: 5 wallclock secs ( 5.08 usr + 0.01 sys = 5.09 CPU) @ 6 +.68/s (n=34) original_pre: 6 wallclock secs ( 5.37 usr + 0.00 sys = 5.37 CPU) @ +46.93/s (n=252)
use Benchmark; my $UID = 0; my $uids = []; my $msgs = []; for (1..10000) { UO->new; } my %pre_uid2msg = map { $_->uid => $_ } @$msgs; timethese(-5, { original => sub { my %uid2msg = map { $_->uid => $_ } @$msgs; return [ map { $uid2msg{$_} } @$uids ]; }, hashslice => sub { my %uid2msg = map { $_->uid => $_ } @$msgs; return [ @uid2msg{ @$uids }]; }, original_pre => sub { return [ map { $pre_uid2msg{$_} } @$uids ]; }, hashslice_pre => sub { return [ @pre_uid2msg{ @$uids }]; } } ); package UO; sub new { $UID += rand(1000); my $self = bless {uid => $UID}, shift(); push(@$uids, $UID); push(@$msgs, $self); return $self; } sub uid { my $self = shift; return $self->{uid}; }
edit (broquaint): changed <pre> tags to <code> tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Ordering objects using external index
by kappa (Chaplain) on Sep 06, 2004 at 21:41 UTC | |
by fergal (Chaplain) on Sep 07, 2004 at 15:30 UTC |