StormTheGates Posted May 27, 2008 Share Posted May 27, 2008 Ive always wondered this. There are two methods that Ive used, one in the past and now the newer one to update things. Here is my old way: <?php $train = mysql_query("SELECT soldiers, guards, workers, scientists FROM followerstats WHERE username='$username' AND family='$family'")or die("Something fucked up, contact Wolf!"); while($success = mysql_fetch_row($train)){ $usold = $success[0]; $uguard = $success[1]; $uwork = $success[2]; $usci = $success[3]; } $usold = $usold + $soldsgot; $uguard = $uguard + $guardsgot; $uwork = $uwork + $workersgot; $usci = $usci = $scisgot; $total = $usold + $uguard + $uwork + $usci; mysql_query("UPDATE followerstats SET soldiers='$usold', guards='$uguard', workers='$uwork', scientists='$usci', total='$total' WHERE username='$username' AND family='$family'")or die(mysql_error()); ?> And here is my new way: [code] <?php $total = $total + $workersgot + $soldiersgot + $guardsgot + $scientistsgot; mysql_query("UPDATE followerstats SET soldiers=soldiers+$soldiersgot, guards=guards+$guardsgot, workers=workers+$workersgot, scientists=scientists+$scientistsgot, total='$total' WHERE username='$username' AND family='$family'")or die(mysql_error()); ?> Which way is faster in the long run do you think? I am considering going through my code and converting things like the first method into things like the second method. Is this a good idea?[/code] Link to comment https://forums.phpfreaks.com/topic/107494-which-is-faster/ Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 Well one is using PHP to do the calculations and the script flows line by line. The other uses MySQL to do it so the script will finish much faster. Is it enough to make a noticable difference? Doubtful. Time it and see. Link to comment https://forums.phpfreaks.com/topic/107494-which-is-faster/#findComment-550981 Share on other sites More sharing options...
Barand Posted May 27, 2008 Share Posted May 27, 2008 To get PHP to do the calculations requires the extra overhead of the select query to get the initial values Link to comment https://forums.phpfreaks.com/topic/107494-which-is-faster/#findComment-550987 Share on other sites More sharing options...
StormTheGates Posted May 27, 2008 Author Share Posted May 27, 2008 So you think I should just do all the minor calculations through the SQL and ditch the selecting part? Link to comment https://forums.phpfreaks.com/topic/107494-which-is-faster/#findComment-551008 Share on other sites More sharing options...
Barand Posted May 27, 2008 Share Posted May 27, 2008 yes, but try time trials and see which is faster http://www.phpfreaks.com/forums/index.php/topic,183317.msg819918.html#msg819918 Link to comment https://forums.phpfreaks.com/topic/107494-which-is-faster/#findComment-551017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.