Jump to content

Clocking the speed of a page load.


Recommended Posts

Ok...I done this on a test game...that im developing..and the time is all wrong...lol....

Let me break it down its on mccode v2

Everypage includes the globals.php. So I figured I would start it there..and put the end on the endpage function which is in everpage...I done this so I would not thave to put it on everypage...

This could be why its doing it not sure..but figured Iwould ask here cause there are some good coders here.

this is the code I'm using now..Also I ahve used 2 different ones but same result..

Start code I put in my globals. Which is included on everypage at the top with:

include "globals.php";


$start = explode(" ", microtime());
$start = $start[1] + $start[0];


Now I put this code in the endpage function in the header.php

But it is located at the bottom of everypage with:



$end = explode(" ", microtime());
$end =$end[1] + $end[0];
$totaltime = ($end - $start);

printf("This page took %f seconds to load.",$totaltiem);


I did not write this code. I got it from www.corecoding.com

So credit for that code goes to them..I say that so no one thinks im trying to take credit for this, lol...

But any ideas why that code would give this result:

This page took 1211880297.858522 seconds to load.

It didnt take that long, lol...but thats the result.



Here is an update...I have made 2 new pages...startspeed.php and endspeed.php..

If i include them at the top and bottom it works right....

But that would be alot of doing to get that on everypage...anyone have an idea of a faster way that would include in another page already included..Like the ones said already..I have concluded though putting it in a function like that is what gave me that error.

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

Change it to microtime(true), then you'll get the string "msec sec", milliseconds then seconds. Use the milliseconds part. IF you use the same code on those included pages, then im not sure, but instead of getting billions of seconds you'll get the (actual) 0.whatever seconds

Edit; also if you want a quick way to append/prepend files to your work, add these two lines in your <dot>htaccess file:

php_value auto_prepend_file start.php

php_value auto_append_file end.php

Just make sure you remember that they're there

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

Ok i will give this a try and post back. Thanks for your Help also :mrgreen:


That didn't work...I guess its not acting right cause its going through the other code or somethign....

I didnt try the htaccess file though cause im not sure what you mean by that..

and that warning after is scarey, lol Make sure i remember its there, lol

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

microtime true only works in php >= 5

go to the php site, and look up microtime, and you'll see a script that's ready made for timing scripts in php 4, and in php 5

it's READY MADE, just copy, paste, and insert the code you wanna time in the middle.

php.net/microtime should get you there

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

Yeah i have tried that code as well...

I have came to the conclusion there is No way to include it in the globals.php and the endpage function...

This is what makes it show a hugh number

Every code words perfect when I use just that code. For example..

I made 2 pages.....


$time_start = microtime(true);






echo "This page came about in $time.";


Now I just include these 2 pages and it will work..Will take more time to put in but this works :)

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

I'm not sure what's making it not work in globals.php and the Header::endpage() method, but there are ways to pass the variables from one place to another. Perhaps you aren't including the appropriate variables in the right places. For instance, through the use of global $blah; or protected $blah and $this-blah.

If you're using php 5, which I assume you are, then in the Header class, you should have some properties defined:

protected $start_time = null;

protected $end_time = null;

in the Header::start_headers() method you should do something like:

$this->start_time = blahblahblah;


in the Header::end_header() method you will now be able to use the $this->start_time property because you've defined it as a protected property of the class and you can refer to it using the reserved variable "$this" as in $this->property.

The $end_time variable would only be used in the Header::end_header() method, but for consitency's sake, I'd make it a property as well, and refer to it as $this->end_time.

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

You don't have to make an addition to the first, or last variables, only the end should the variables be split into arrays, and subtracted from each other; it gives the same result.

$page_load = array();
$page_load['start'] = microtime();

{ ... }

$page_load['start'] = explode(' ', $page_load['start']);
$page_load['end'] = explode(' ', microtime());
$page_load = $page_load['end'][1] - $page_load['start'][1];
echo 'Page has been generated in ' . round($page_load, 3) . ' seconds.';


As to how to use it in a class, here's an example: http://www.webdigity.com/index.php?acti ... al;code=59

Link to comment
Share on other sites

Re: Clocking the speed of a page load.

To save changing all the pages I used a session:

In the header page in the function 'startheaders()'

$starttime = explode(' ', microtime());
$_SESSION['starttime'] = $starttime[1] + $starttime[0];


Then in the end page part:

$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
Page Generated in %f seconds.', $totaltime);


Although there's probably a better way of doing this it works this way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...