iPixel Posted November 13, 2009 Share Posted November 13, 2009 I've got a few nested loops going. They are loops that pull data from a database and then depending on the query results run other loops based on that data. ie: $sql = "SELECT firstname, lastname FROM tablename WHERE branch= $_POST[branch]"; while(row = mysql_fetch..blahblahblah) { sql2 = "SELECT count_pnums, dist_pnums FROM tablename WHERE sdate BETWEEN '00-00-00' AND '00-00-00' and lastname = 'somebodys name'; while while(row2 = mysql_fetch..blahblahblah) { do more stuff here based on results. } } So what i wanna know is, is it possible to somehow check how long each loop takes to complete all the cycles based on results? PS: sorry i hit tab enter and it posted instead of indenting code hehe. Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/ Share on other sites More sharing options...
Mchl Posted November 13, 2009 Share Posted November 13, 2009 And your question is? Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957003 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 You mean this? $time_start = microtime(true); //Your code here $time_end = microtime(true); $time = $time_end - $time_start; echo "Finished in $time"; Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957006 Share on other sites More sharing options...
iPixel Posted November 13, 2009 Author Share Posted November 13, 2009 Well sort of, but that's kinda counts the time for the whole code to run, i want to count each loop individually, would i be able to use that for my task? Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957008 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 Well sort of, but that's kinda counts the time for the whole code to run, i want to count each loop individually, would i be able to use that for my task? Sure, //loop start //timer start //do stuff //timer end -- save/echo time elapsed //loop end Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957010 Share on other sites More sharing options...
Mchl Posted November 13, 2009 Share Posted November 13, 2009 Yes, just start and stop measuring time in different places... Anyway, running queries in a loop is not an efficient way. Perhaps you can retrieve same data using SQL JOINs? It's usually much faster. Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957012 Share on other sites More sharing options...
iPixel Posted November 13, 2009 Author Share Posted November 13, 2009 But since i want to check the entirety of my loop not just 1 run through, wouldnt it be // timer start // loop start // loop end // timer end // singletime = timer end - timer start // totaltime += singletime ?? if that makes sense?! Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957013 Share on other sites More sharing options...
iPixel Posted November 13, 2009 Author Share Posted November 13, 2009 Yes, just start and stop measuring time in different places... Anyway, running queries in a loop is not an efficient way. Perhaps you can retrieve same data using SQL JOINs? It's usually much faster. I've been trying to think of ways to avoid looping with queries, but i'm not exactly a genius at that "YET" Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957014 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 But since i want to check the entirety of my loop not just 1 run through, wouldnt it be // timer start // loop start // loop end // timer end // singletime = timer end - timer start // totaltime += singletime ?? if that makes sense?! This is the same concept as a stopwatch. If the timer is inside the loop, it will record the time it takes for one iteration of your loop If it is outside your loop, it will record the time it takes for the entire loop to complete. Whatever you want to time, you place between the timer start and end. Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957016 Share on other sites More sharing options...
iPixel Posted November 13, 2009 Author Share Posted November 13, 2009 TY! Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957018 Share on other sites More sharing options...
Mchl Posted November 13, 2009 Share Posted November 13, 2009 I've been trying to think of ways to avoid looping with queries, but i'm not exactly a genius at that "YET" Maybe it's high time you've become one? It's not that difficult. Sure, it takes some more thinking, but the final effect is worth it. Quote Link to comment https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/#findComment-957022 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.