Jump to content

Recommended Posts

guys? which do you think among this two codes execute faster.

 

Code 1:

<?php
$query = @mysql_query("SELECT fld1,fld2,fld3 FROM tablename");
while($row = @mysql_fetch_array($query)) { 
   echo $row['fld1']."<br>";
   echo $row['fld2']."<br>";
  echo $row['fld3']; 
}
?>

 

Code 2:

<?php
$query = @mysql_query("SELECT fld1,fld2 FROM tablename");
while($row = @mysql_fetch_array($query)) { 
   echo $row['fld1']."<br>";
   echo $row['fld2'] * 1.12; 
}
?>

 

How do i also get the difference in their execution time?

 

Tnx....

Link to comment
https://forums.phpfreaks.com/topic/242401-php-executime-time-difference/
Share on other sites

I doubt you'll see any significant difference, unless it's a very large table, and fld1 and fld2 can be fetched in some faster way such as from an index.  So the short answer is in most cases, there's no significant difference.

 

If you want to benchmark them, I would run each query at least 1000 times and measure the total time, using php's microtime().  You might want to run each query once before you benchmark, to make sure everything is cached for both runs.  And if you're serious about optimizing queries, you would want to learn about and use "EXPLAIN": http://dev.mysql.com/doc/refman/5.0/en/explain.html

If you are working on a big table though, the second one is most likely going to be faster (but still, very slightly). The first code gets extra persistent storage and loads it into memory. The second one simply does some binary manipulation with the memory without calling extra persistent storage. PHP tends to be faster than MySQL.

 

Plus, if you have queries elsewhere that update that table/insert entries, adding more fields will of course have a performance impact.

 

All in all, you really shouldn't be too concerned about it, unless you really have to worry about performance (an online gake/MMO/corporate website). The difference will probably be in microseconds, which are extremely small.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.