Network_ninja Posted July 20, 2011 Share Posted July 20, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/242401-php-executime-time-difference/ Share on other sites More sharing options...
btherl Posted July 20, 2011 Share Posted July 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242401-php-executime-time-difference/#findComment-1244959 Share on other sites More sharing options...
Cless Posted July 20, 2011 Share Posted July 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242401-php-executime-time-difference/#findComment-1244961 Share on other sites More sharing options...
Network_ninja Posted July 20, 2011 Author Share Posted July 20, 2011 tnx to the very quick reply.... so i would go to the code number 2. even if the difference in time is very slight.... tnx tnx tnx..... Quote Link to comment https://forums.phpfreaks.com/topic/242401-php-executime-time-difference/#findComment-1244969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.