Support Forums

Full Version: [HOWTO] Check memory usage and loading time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Let me show you a simple process, a way to get memory amount that your script uses, and also
how much time it needs to execute.
PHP has two functions built in that we will use for that.
The first will be microtime(), and the second memory_get_usage().
The function microtime() returns a current Unix timestamp with microseconds (from php.net)
If you pass true as a argument to microtime(), you'll get the timestamp in float format.

The function memory_get_usage(), if called with a true as an argument, will return the amount of memory
that is being used by the entire PHP engine, otherwise it returns the amount that is being used by the script.

Way to do it!


When you start writting a script, you start with these two lines , and end it with the ending lines the script below.
PHP Code:
<?php

$start_time 
=  microtime();
$start_mem memory_get_usage();

/*
 * your script instructions between the 2 calls to those functions
 */

$end_time = (microtime() - $start_time);
$end_mem = (memory_get_usage() - $start_mem);

echo 
"Memory Usage: "round((($end_mem 1024) / 1024), 6), " MB <br />";
echo 
"Execution Time: "round($end_time6), " seconds<br />";

?>

We call both functions without arguments.
We have now declared variables, $start_time and $start_mem.
$start_time holds the timestamp for the execution start.
$start_mem holds the amount of memory (in bytes) that is currently being used by the script, since it's a start now much
memory is being allocated.

As soon as we start adding new functions, variables and control statements, the value returned by above
two functions will differ from previous ones!

So let us put some weight on to our script.
A simple for loop like this;
PHP Code:
<?php

$start_time 
=  microtime();
$start_mem memory_get_usage();

for(
$i 0$i 1000$i++)
{
    echo 
$i;
}

$end_time = (microtime() - $start_time);
$end_mem = (memory_get_usage() - $start_mem);

echo 
"Memory Usage: "round((($end_mem 1024) / 1024), 6), " MB <br />";
echo 
"Execution Time: "round($end_time6), " seconds<br />";

?>

The data that I get is, Memory Usage: 0.000244 MB and Loading Time: 0.001339 seconds.
If we now add more things to our script, like this;

PHP Code:
<?php
$start_time 
=  microtime();
$start_mem memory_get_usage();

for(
$i 0$i 1000$i++)
{
    echo 
$i;
}


$v file_get_contents(__FILE__);
if(
$v == false)
{
    die(
"");
}
else
{
    
$v htmlentities($vENT_QUOTES);
}

$end_time = (microtime() - $start_time);
$end_mem = (memory_get_usage() - $start_mem);

echo 
"Memory Usage: "round((($end_mem 1024) / 1024), 6), " MB <br />";
echo 
"Execution Time: "round($end_time6), " seconds<br />";

?>

I get, Memory Usage: 0.00145 MB and Loading Time: 0.002297 seconds.
As you can see the amount of memory has increased tiny bit and also did the execution time, the bigger your script,
more memory and time it needs to execute.

You can also use this structure to measure how much weight your functions have.
Here is an example;

PHP Code:
<?php

function exec_weight($time 0$mem 0)
{
    
$time =  ($time !== 0) ? (microtime() - $time) : microtime();
    
$mem = ($mem !== 0) ? (memory_get_usage() - $mem) : memory_get_usage();
    
    return array(
$time$mem);
}

function 
parseHTML($str)
{
    
$data exec_weight();
    
$str htmlentities($strENT_QUOTES);
    
$data exec_weight($data[0], $data[1]);
    return 
$data;
}

$exec parseHTML(file_get_contents(__FILE__));

echo 
"Memory Usage: "round((($exec[1] / 1024) / 1024), 6), " MB <br />";
echo 
"Execution Time: "round($exec[0], 6), " seconds<br />";

?>

If you change the function parseHTML, add this line to it;
PHP Code:
    $str file_get_contents($str);
    
//
function parseHTML($str)
{
    
$data exec_weight();
    
$str file_get_contents($str);
    
$str htmlentities($strENT_QUOTES);
    
$data exec_weight($data[0], $data[1]);
    return 
$data;
}

$exec parseHTML(__FILE__); 

You will notice a raise in memory usage and execution time, the function has more instructions and neeeds more time and
memory to process that.

That's it, you now know how to check if your script needs a diet.
Nice tutorial, thank you.
Wow Big Grin
Cool tutorial dude Smile
Keep it up !
That's a nice snippet. I'm going to experiment with this.
Thanks guys....
Just something I forgot to put in it.

PHP Code:
function parseHTML($str)
{
    
$data exec_weight();
    
$str file_get_contents($str);
    
$str htmlentities($strENT_QUOTES);
    
$data exec_weight($data[0], $data[1]);
    return 
$data;
}
// Should BE
function parseHTML(&$str)
{
    
$data exec_weight();
    
$str file_get_contents($str);
    
$str htmlentities($strENT_QUOTES);
    
$data exec_weight($data[0], $data[1]);
    return 
$data;


Notice the & in the changed version, parseHTML(&$str)
This is called Passing by Reference, it simply means that the variable passed to the function, will be updated by the function, no need for returning changed content.
Since the function parseHTML only returns memory usage and execution time, changes to $str don't really happen, since we are not getting them back or outputing them in any way.
Wow man really nice work!

I especially like the page loading time script.

Thanks a bunch Big Grin
Thats really helpful...thanks mate, keep it up!