Languages :: PHP :: slow process |
|||
| By: roe1and |
Date: 05/02/2008 10:51:11 |
Points: 20 | Status: Answered Quality : Excellent |
|
hi vgr. this question is re: my earlier one. what i am doing is loading the results of a unix du -ak into a database. so in my code below du_size is a number(usually between 50 and 300). all i want to do is add them up. but it seems my method is quite slow. any ideas? echo '<table>'; $sql3 = "SELECT du_size FROM $tbl_du;"; $query3 = mysql_query($sql3, $marc) or die ("No 3: Misbehaving: ".mysql_error()); $rows3 = mysql_num_rows($query3); $du_size_total = 0; for ($i=0; $i<$rows3; $i++) { $du_size = mysql_result($query3, $i, "du_size"); $du_size_total = $du_size + $du_size_total; } echo '<tr><td>' . $du_size_total . ' total size in bytes</td></tr>'; echo '</table>'; mysql_free_result($query3); |
|||
| By: VGR | Date: 07/02/2008 08:49:37 | Type : Answer |
|
| yes, it's ugly slow because you are doing the exact opposite of the recommendations on http://fr3.php.net/manual/en/function.mysql-result.php ;-)
in your case, you want to addthe du_size column value for all lines : why don't you do simply select sum(du_size) as theresult from $tbl_du; ? and then do a single $res=mysql_fetch_array(); and get $res['theresult']... Moreover, you do a loop for all lines while you ask already for the complete dataset : so, in fact, and unless I'm mistaken - I always use fetching functions, never that mysql_result() form... - you are operating in O(n²) in stead of O(n) !!! This explains why it's so slooowwww.... |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








