Note that there are some explanatory texts on larger screens.

plurals
  1. POBenchmark memory usage in PHP
    primarykey
    data
    text
    <p>SO,</p> <p><strong>Specifics</strong></p> <p>Let us suppose that we have some problem and at least two solutions for it. And what we want to achieve - is to compare effectiveness for them. How to do this? Obviously, the best answer is: <em>do tests</em>. And I doubt there's a better way when it comes to language-specific questions (for example "what is faster for PHP: <code>echo 'foo', 'bar'</code> or <code>echo('foo'.'bar')</code>").</p> <p>Ok, now we'll assume that if we want to test some code, it's equal to test some function. Why? Because we can wrap that code to function and pass it's context (if any) as it's parameters. Thus, all we need - is to have, for example, some benchmark function which will do all stuff. Here's very simple one:</p> <pre><code>function benchmark(callable $function, $args=null, $count=1) { $time = microtime(1); for($i=0; $i&lt;$count; $i++) { $result = is_array($args)? call_user_func_array($function, $args): call_user_func_array($function); } return [ 'total_time' =&gt; microtime(1) - $time, 'average_time' =&gt; (microtime(1) - $time)/$count, 'count' =&gt; $count ]; } </code></pre> <p>-this will fit our issue and can be used to do <em>comparative</em> benchmarks. Under <em>comparative</em> I mean that we can use function above for code <code>X</code>, then for code <code>Y</code> and, after that, we can say that code <code>X</code> is <code>Z%</code> faster/slower than code <code>Y</code>.</p> <p><strong>The problem</strong></p> <p>Ok, so we can easily measure time. But what about memory? Our previous assumption <em>"if we want to test some code, it's equal to test some function"</em> seems to be not true here. Why? Because - it's true from formal point, but if we'll hide code inside function, we'll never be able to measure memory after that. Example:</p> <pre><code>function foo($x, $y) { $bar = array_fill(0, $y, str_repeat('bar', $x)); //do stuff } function baz($n) { //do stuff, resulting in $x, $y $bee = foo($x, $y); //do other stuff } </code></pre> <p>-and we want to test <code>baz</code> - i.e. how much memory it will use. By 'how much' I mean <em>'how much will be maximum memory usage during execution of function'</em>. And it is obvious that we can not act like when we were measuring time of execution - because we know nothing about function outside of it - it's a black box. If fact, we even can't be sure that function will be successfully executed (imagine what will happen if somehow <code>$x</code> and <code>$y</code> inside <code>baz</code> will be assigned as 1E6, for example). Thus, may be it isn't a good idea to wrap our code inside function. But what if code itself contains other functions/methods call? </p> <p><strong>My approach</strong></p> <p>My current idea is to create somehow a function, which will measure memory <em>after each input code's line</em>. That means something like this: let we have code</p> <pre><code>$x = foo(); echo($x); $y = bar(); </code></pre> <p>-and after doing some thing, measure function will do:</p> <pre><code>$memory = memory_get_usage(); $max = 0; $x = foo();//line 1 of code $memory = memory_get_usage()-$memory; $max = $memory&gt;$max:$memory:$max; $memory = memory_get_usage(); echo($x);//second line of code $memory = memory_get_usage()-$memory; $max = $memory&gt;$max:$memory:$max; $memory = memory_get_usage(); $y = bar();//third line of code $memory = memory_get_usage()-$memory; $max = $memory&gt;$max:$memory:$max; $memory = memory_get_usage(); //our result is $max </code></pre> <p>-but that looks weird and also it does not answer a question - how to measure function memory usage.</p> <p><strong>Use-case</strong></p> <p>Use-case for this: in most case, complexity-theory can provide at least <code>big-O</code> estimation for certain code. But:</p> <ul> <li>First, code can be huge - and I want to avoid it's manual analysis as long as possible. And that is why my current idea is bad: it can be applied, yes, but it will still manual work with code. And, more, to go deeper in code's structure I will need to apply it recursively: for example, after applying it for top-level I've found that some <code>foo()</code> function takes too much memory. What I will do? Yes, go to this <code>foo()</code> function, and.. repeat my analysis within it. And so on.</li> <li>Second - as I've mentioned, there are some language-specific things that can be resolved only by doing tests. That is why having some automatic way like for time measurement is my goal.</li> </ul> <p>Also, garbage collection is enabled. I am using PHP 5.5 (I believe this matters)</p> <p><strong>The question</strong></p> <p>How can we effectively measure memory usage of certain function? Is it achievable in PHP? May be it's possible with some simple code (like <code>benchmark</code> function for time measuring above)? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload