[HOWTO] Check memory usage and loading time - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: [HOWTO] Check memory usage and loading time (/showthread.php?tid=16078) |
[HOWTO] Check memory usage and loading time - Gaijin - 02-08-2011 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 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 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 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 If you change the function parseHTML, add this line to it; PHP Code: $str = file_get_contents($str); 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. RE: [HOWTO] Check memory usage and loading time - versx - 02-10-2011 Nice tutorial, thank you. RE: [HOWTO] Check memory usage and loading time - w00pz - 02-11-2011 Wow Cool tutorial dude Keep it up ! RE: [HOWTO] Check memory usage and loading time - PaperBag - 03-03-2011 That's a nice snippet. I'm going to experiment with this. RE: [HOWTO] Check memory usage and loading time - Gaijin - 03-05-2011 Thanks guys.... Just something I forgot to put in it. PHP Code: function parseHTML($str) 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. RE: [HOWTO] Check memory usage and loading time - sparkā¢ - 04-25-2011 Wow man really nice work! I especially like the page loading time script. Thanks a bunch RE: [HOWTO] Check memory usage and loading time - harris21 - 04-26-2011 Thats really helpful...thanks mate, keep it up! |