I've inherited some code at work that does some database updates. Basically does a $dbh->do() in the middle of a loop. Thousands of times. For testing, I just redirected the Mail output (w/sendmail) to a text file.
#old.pl while (@emails = $sth->fetchrow) { $id = $emails[0]; $subject = $emails[1]; $body = $emails[2]; $mailto = $emails[3]; $mailfrom = $emails[4]; $type = $emails[5]; push (@emailsToSend,"To: $mailto\nFrom: $mailfrom\nSubject: $subje +ct\n\n$body\n"); push (@emailIds,$id); push (@fromAddys,$mailfrom); if ($type eq 'weekly') { $totalweekly++; } elsif ($type eq 'daily') { $totaldaily++; } elsif ($type eq 'blast') { $totalnewsBlast++; } elsif ($type eq 'news') { $totalnews++; } } $emailcount = 0; foreach $singleEmail (@emailsToSend) { # SEND THE EMAIL $mailfrom = $fromAddys[$emailcount]; open (OUT, ">>oldEmail.txt"); print OUT $singleEmail; close(OUT); $dbh->do(qq[UPDATE shawnTest set sent = 'Y' WHERE emailId=$emailId +s[$emailcount]]) || &ErrorAlert("500E emailmonitor.pl DBI do error: ".DBI->errstr); $emailcount++; }
This seemed like quite a bit of overhead, with the extra variable assignment, as well as the DBI->do, so I did something like this:
#new.pl my %total=map{$_,0}(qw(weekly daily blast custom)); my $updateSql=qq[UPDATE shawnTest set sent='Y' WHERE emailId=?]; my $update=$dbh->prepare($updateSql); while(@emails = $sth->fetchrow_array()) { open (OUT, ">>newEmail.txt"); print OUT "To: $emails[3]\nFrom: $emails[4]\nSubject:$emails[1 +]\n\n$emails[2]\n"; close OUT; $update->execute($emails[0]); $total{$emails[5]}++; }
In both files, this code lives in a sub called fetchemail. I added some code like this to both files.
my $t0 = new Benchmark; fetchmail(); my $t1 = new Benchmark; my $td = timediff($t1, $t0); print "the code took:",timestr($td),"\n";
perl old.pl
the code took: 9 wallclock secs ( 1.43 usr 1.55 sys + 0.02 cusr 0.02 csys = 0.00 CPU)

#Reset database perl new.pl
the code took: 9 wallclock secs ( 1.59 usr + 1.58 sys = 3.17 CPU)

This code did 10,000 updates, so I didn't feel it necessary to Benchmark it over a thousand times or so. But I did expect, in light of the placeholders, for the results to be a bit more different. Is it my understanding of Benchmark (lack thereof), or do I underestimate the power of placeholders?

ÅßÅ×ÅßÅ
"It is a very mixed blessing to be brought back from the dead." -- Kurt Vonnegut


In reply to Unexpected Benchmark results with DBI-placeholders by abaxaba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.