The page is generated for...

All of kind time of day!


In it to small clause{article} I want to tell to you how to note down the time performance of your code on PHP. Many name this reception by calculation of time of generation of page. This name also it pleased me, so further I shall use it  more often.


Do not speak me, that you never saw approximately such inscription on sites:

The page is generated for 0.235467 seconds



Or

Page generated in 0.235467 seconds



Prikol`naja a thing, however. I shall not be mistaken, if you too want to realize her  on the site? Want - then have gone.


First I shall explain logic of job of such chesspiece. She is very simple:

?         Before performance of that code which we want to mark, we read - out current time.

?         After performance of a code, besides we read - out current time and we deduce{remove} a difference of turned out times.


I recommend at once the parts of a script responsible for calculation of time in the beginning and at the end of a code, accordingly, to bear{take out} in separate files and to insert of them as required.


So, a code of a script start.php which counts up current time (to insert in that place, whence we begin calculation, i.e. somewhere in the beginning of page).

<?

* ------a file start.php


// We read - out current time

$start_time = microtime ();

// We divide{share} seconds and milliseconds (become values of initial keys of the file - list)

$start_array = explode (" ", $start_time);

// It also is starting time

$start_time = $start_array [1] + $start_array [0];

?>



Starting time have counted up. Now the code of a file end.php which also counts up current time, and then deduces a difference of this time from counted up in a file start.php, i.e., as a matter of fact, and deduces time of generation of page.

<?

* ------a file end.php


// We do{make} the same, as in start.php, only we use other variables

$end_time = microtime ();

$end_array = explode (" ", $end_time);

$end_time = $end_array [1] + $end_array [0];

// We deduct final time initial

$time = $end_time - $start_time;

// We deduce{remove} in a target stream (browser) time of generation of page

printf (" the Page is generated for %f seconds ", $time);

?>



Basically, all. A file start.php insert there, whence want to start to note time; end.php - where want " to stop your stop watch ". Inserts of files can carry out, for example, the instruction include ();


Done... However, for certain you want to understand, as that we have written works. Personally I do not like juzat` another's operating time basically. And if and juzaju, that, necessarily having understood their job. Further I shall explain to you for what functions which we applied in examples serve and I shall give some advice.

The functions used in examples


string microtime () - returns a line in a format: " micro seconds of second ", in which seconds - timestamp, returned by function time (), and micro seconds - a fractional part of seconds, the employee for more exact measurement of time intervals. Function works only in systems, which support a system call gettimeofday (), i.e. practically in all.


The remark: timestamp - a format of time which is equaled " kol-vu the seconds past{last} since midnight on January, 1, 1970 across Greenwich till the present{true} moment ". This data format is accepted in axes UNIX, as standard. Universal and convenient performance which you yet time will collide{face}.


array explode (string separator, string string [, int limit]) - receives a line set in its{her} second argument and tries to find in her podstroki, equal to the first argument. Then in a place of ocurrence of these podstrok a line "razrezaetsja" on the parts placed in the file - list which comes back. If the parameter limit the first are taken into account only limit-1 sites of "cut"{"section"} is set. Thus, the list from no more than limit elements comes back.


The remark: string implode (string glue, array pieces) (a synonym - join ()) - kh-¿, completely opposite on value kh-¿ explode (). They take an associative file (as a rule, it it is the list) pieces and stick together to his  value in a uniform line with the help of "line - glue" glue.


void printf (string format [, mixed args]) - kh-n, completely similar to the C versions. She deduces in a browser a line made on the basis of a line of formatting, containing some special symbols which subsequently will be replaced with values of corresponding variable arguments from the list. Parameters:

format - line a format of a conclusion of the data

args - arguments for formatting

I shall not stop in detail on this function, as its{her} full description volumetric enough (well not too that...), and function very powerful (sluggish including). I shall say only, that in our case we use her  that at a conclusion to transform turned out " time of generation of page " up to number with a floating point (x.xxxxxx). I.e. that after a point contained no more than 6 symbols.


The remark: it was possible and use a simple call of function such as echo () or print (). However in this case, the number with a plenty of figures after a floating point will be deduced{removed}!

Advice{Council}


Examples can be used for zasekanija performance of any skriptovogo a fragment. I.e., that on a site generation of dynamical page is marked completely is absolutely unessential. The some people swindle, marking absolutely not understandably that; therefore to not have to be surprised, that a site, gruzjahhijsja with speed of a cockroach, it is generated for surprisingly small time. Basically, time of generation and time of a conclusion of page - absolutely different things. For example, on our site - Progers.ru we note time of performance of all code PHP which is present.


, perhaps and everything, that I wanted you to tell concerning this subject. A typical example can see on our site - Progers.ru where in copyrights this notorious time of generation of page is deduced.


If you have found oshibki\netochnosti in clause{article} or simply want to share impressions, al` to ask me about something on this subject write or leave here the comments.