Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/181419-solved-how-can-i-time-my-loops/
Share on other sites

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"

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.

 

 

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? :P

It's not that difficult. Sure, it takes some more thinking, but the final effect is worth it.

 

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.