FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP script execution time





Gregoric
Hi there

Just as the topic says, I am searching how to let the PHP script return how long did it take to execute. I have a PHP page which is used with AJaX and I would like it to produce the data it should and how long did it take.

Only way that comes to my mind to achieve that is to save time at script's start and then compare it with time at the end. The difference would be my desired variable.

Any other ideas? I am not sure if the time acquired with method above would be accurate.

Regards
jmraker
Gregoric wrote:
Only way that comes to my mind to achieve that is to save time at script's start and then compare it with time at the end. The difference would be my desired variable.


That's exactly how it's usually done, but instead of the usual time functions like time()
the function microtime() does a nice job

http://php.net/manual/en/function.microtime.php
http://www.phpjabbers.com/measuring-php-page-load-time-php17.html

a very useful function
Code:
function microtime_float() {
  list($usec, $sec) = explode(" ", microtime());
  return ((float)$usec + (float)$sec);
}


You'd want to get the start time as soon as possible in the script as possible, before any includes.

It can also be used to time other things within the program like mysql execution time to find out how long each or one query takes down to the microsecond, or to time a function call.

Don't worry if the time difference varies at certain times. If the server is busy or stressed the amount of time waiting for I/O goes up, and it can accumulate to a second or 2 more in execution time where normally it's much less than 1/10 of a second
badai
this is what i usually do:

at the beginning of my script:
Code:
$start_time = time();


end of script (or when the script die/return), assume your ajax accept json as result:
Code:
$end_time = time();
echo json_encode(array("success" => true, "running_time" => sec2hms($end_time - $start_time, true), "your_other_result" => $whatever));


the sec2hms function will give you a nice result like 00:03:40 for 3 minutes 40 seconds

this is sec2hms function:
Code:
function sec2hms ($sec, $padHours = false)
  {

    // start with a blank string
    $hms = "";
   
    // do the hours first: there are 3600 seconds in an hour, so if we divide
    // the total number of seconds by 3600 and throw away the remainder, we're
    // left with the number of hours in those seconds
    $hours = intval(intval($sec) / 3600);

    // add hours to $hms (with a leading 0 if asked for)
    $hms .= ($padHours)
          ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
          : $hours. ":";
   
    // dividing the total seconds by 60 will give us the number of minutes
    // in total, but we're interested in *minutes past the hour* and to get
    // this, we have to divide by 60 again and then use the remainder
    $minutes = intval(($sec / 60) % 60);

    // add minutes to $hms (with a leading 0 if needed)
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";

    // seconds past the minute are found by dividing the total number of seconds
    // by 60 and using the remainder
    $seconds = intval($sec % 60);

    // add seconds to $hms (with a leading 0 if needed)
    $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);

    // done!
    return $hms;
   
  }
Gregoric
Thanks for those two answers.

Badai, is this your own created function? I mean the sec2hms?

Anyway, thanks for telling me about json. I've been reading about it but didn't find any serious use for it (as for now, I am new and inexperienced to the web dev). Now, I'll read even more any try to implement it into my projects.

Do you know any tutorial or explanation of the json? I can manage myself with Google but maybe you know some hidden, brilliant article Smile. Anything not W3Schools.
badai
Gregoric wrote:
Thanks for those two answers.

Badai, is this your own created function? I mean the sec2hms?

Anyway, thanks for telling me about json. I've been reading about it but didn't find any serious use for it (as for now, I am new and inexperienced to the web dev). Now, I'll read even more any try to implement it into my projects.

Do you know any tutorial or explanation of the json? I can manage myself with Google but maybe you know some hidden, brilliant article Smile. Anything not W3Schools.


hell no! i never reinvent the wheel.

about json, there is nothing to explain really.

when you write an object, you have properties and values. just write it.

myObject = {properties1: value1, properties2: value2, properties3: value3}

that's the simplest object. so simple that it don't have any methods. and some smart people figure out that you can use that to exchange information.

like a morse code, some smart people figure out that you can use dots and dashes to information. and now other smart people figure out that a plain "object notation" (how you write an object) can also be use to exchange information.

confuse?

first know that you use something to communicate. voice, words. smoke, xml, whatever
then know what an object is.
how to declare an object. a very simple object. like you declare some variable.
() for parameter, [] for array and {} for object
then, the way you declare that object in javascript is called JSON - javascript object notation
got it so far? if not start again until you grasp what is JSON. it's the way you write an object literally in javascript.

then just use JSON to exchange information.

see, nothing to explain really.
Gregoric
Okay then, thanks.

That is what I really though about JSON. However, when first read about it and it seemed so easy (and universal) to use - I just passed by and forgot about it. Till now.

Thanks again. Now I have few more questions but that's wider topic and will create thread on the forums, soon Smile. Regards
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script- How can we install it
I need a VERY simple php script
PHP Script needed
Logging systeem php script
Preventing bad PHP script calls
array check box on php script using $_POST
How to change extension php to html but the php script still
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.