Be careful with your benchmarks. You're doing so much in the loop that
any diference between the two methods is "lost in the noise". The cost
of a print, for example, is relatively high and variable (buffering
and screen/file i/o etc).
And besides, the difference between the methods is so small that it's
only remotely relevant when fetching very large numbers of rows and
doing very little with them within the loop.
Beware premature optimization. If it's not hurting, don't "fix" it.
Tim.
On Fri, Sep 07, 2007 at 02:33:26PM +0800, Ow Mun Heng wrote:
> Connection is to a MSSQL Server using DBI:sybase.
>
> queries are the same, difference is one uses fetchrow_array() and the
> other uses fetchrow_arrayref().
>
> Supposedly, fetrow_arrayref() is faster than the other but doesn't seem
> that way.
> (this is per Tim Bunce - Advanced DBI Tutorial July 2007)
>
> Query obtained a total of ~70 columns. (output diverted to /dev/null)
>
> Average of Actual Time Process Time
> Fetchtype real sys user Grand Total
> aray 5.3462 0.5484 3.128 3.007533333
> arrayref 6.5944 0.878 2.696 3.389466667
> Grand Total 5.9703 0.7132 2.912 3.1985
>
>
> while ( $first = $sth->fetchrow_arrayref )
> {
> my $count = @$first;
> for (my $counter = 0; $counter < $count; $counter = $counter + 1)
> {
> if ($counter == $count-1)
> {
> if (defined($first->[$counter]))
> {
> print "\"$first->[$counter]\"";
> }
> } else {
> if (!defined($first->[$counter]))
> {
> print ",";
> } else {
> print "\"$first->[$counter]\",";
> }
> }
> }
> print "\n";
> }
>
> vs :
>
> while ( @first = $sth->fetchrow_array )
> {
> my $count = @first;
>
> for (my $counter = 0; $counter < $count; $counter = $counter + 1)
> {
> if ($counter == $count-1)
> {
> if (defined($first[$counter]))
> {
> print "\"$first[$counter]\"";
> }
> } else {
> if (!defined($first[$counter]))
> {
> print ",";
> } else {
> print "\"$first[$counter]\",";
> }
> }
> }
> print "\n";
> }
>
>