On May 21, 5:38 pm, Dustin <dsalli...@gmail.com> wrote: .. > > I can't imagine why it wouldn't be worse. On each iteration, you > compile a SQL query then block on network activity 20 times in the > memcached case. You're doing almost the entire SQLite workload in the > memcached case and then mixing in a gang of network round trips. > > In the SQLite case, you should've expected a performance gain since > you're compiling queries less frequently than you did in the previous > version. > > memcached is a network service. You'd never structure code such > that the inner loop of your program requires a full round trip over > the network. Perform a single get for all of the keys you want and if > you miss any, compile the query at that time, backfill them, and fill > in the holes.
Nice. I changed the code to sub { my $sth = $dbh->prepare($sql); my @res = (); my $hashref = $memd->get_multi(@ids); while (my ($id, $str) = each %$hashref) { unless ($str) { $sth->execute($id); ($str) = $sth->fetchrow_array; $memd->set($id, $str); } push @res, [$_, $str]; } out(@res); } and now I get Benchmark: timing 10000 iterations of query_dbh, query_mem... query_dbh: 6 wallclock secs ( 3.28 usr + 1.03 sys = 4.31 CPU) @ 2320.19/s (n=10000) query_mem: 10 wallclock secs ( 6.57 usr + 1.45 sys = 8.02 CPU) @ 1246.88/s (n=10000) instead of Benchmark: timing 10000 iterations of query_dbh, query_mem... query_dbh: 6 wallclock secs ( 3.29 usr + 1.03 sys = 4.32 CPU) @ 2314.81/s (n=10000) query_mem: 54 wallclock secs (30.02 usr + 8.24 sys = 38.26 CPU) @ 261.37/s (n=10000) an almost 5 times improvement.