Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory consumption on an iteration script
    primarykey
    data
    text
    <p>I have a script written in PHP and the same script written in Javascript.<br/> It iterates a million times and on each time strips a string into an array and assigns the first array item into a new variable.</p> <p>The PHP is:</p> <pre><code>class First { public function Iterate() { $count = 1000000; $test_string = ''; $test_array = ''; $first_word = ''; for($i=1; $i &lt;= $count; $i++){ $test_string = 'This is a test string'; //could use explode but no explode in js $test_array = split(" ", $test_string); $first_word = $test_array[0]; } } } $first = new First(); $first-&gt;Iterate(); </code></pre> <p>And the Javascript is:</p> <pre><code>function First() { this.Iterate = function() { count = 1000000; test_string = ''; test_array = ''; first_word = ''; for(var i=1;i &lt;= count; i++){ test_string = 'This is a test string'; test_array = test_string.split(" "); first_word = test_array[0]; } } } first = new First(); first.Iterate(); </code></pre> <p>I run PHP with PHP-CLI 5.3.10 and the Javascript with node v0.6.12.<br/></p> <p>For PHP I use 'memory_get_usage()' and for Javascript I use 'process.memoryUsage()'. I run them at the start of the script, then at the end, then minus end with start and finally convert the number of bytes into mb.</p> <p>The PHP uses 0.00065 mb of memory whereas Javascript uses 0.25 mb however PHP takes 4 secs and Javascript takes 0.71 secs. I have run the results on 2 different machines.<br/> </p> <p>Does anybody know why the Javascript memory usage would be so much higher than the PHP's (despite the fact that the Javascript is executed so much faster)?</p> <p>The only explanation I could come up with was the V8's nature to use Hidden classes improves speed but increases memory consumption.</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