Você pode ler este post em português também.
Hello!
The April article was released at May. The May one, will be released at May too? 😉
No more talking. This post compares the performance of each way to assign a value to an array:
- Simple assign (ex: $a[] = 1);
- By array_push function (ex: array_push($a, 1));
In PHP website:
I think that a performance test of the two ways shows how much better is to free the HEAP area from functions calls. Let’s know the test tools:
- Computer: Dell Optiplex 755;
- OS: Fedora GNU/Linux 12, X86_64 architecture;
- PHP: Version 5.3.2, from Fedora repositories;
- Environment: Bash, no X. EXT4 file system;
- File array.php:
<?php $a = array(); for ($i = 0; $i < 1000000; $i++) { $a[] = 1; }?>
- File array_push.php:
<?php $a = array(); for ($i = 0; $i < 1000000; $i++) { array_push($a, 1); } ?>
- Execute each of the PHP scripts 100 times, saving the execution time in each iteration:
for i in `seq 1 100`; do /usr/bin/time -f "%e" php array.php 2>> array.log; done;
for i in `seq 1 100`; do /usr/bin/time -f "%e" php array_push.php 2>> array_push.log; done;
Obs.:The PC was turned off by 30 seconds between each test. - Order the execution times, from smallest to largest;
- Throw away the 25 first and the 25 last execution times;
- Plot the 50 remaining results.
- Execute each of the PHP scripts 100 times, saving the execution time in each iteration:
Method:
Conclusion: Perform a code review changing array_push calls by simple assigns will give you few seconds faster. So do it right now ;-).
Regards!
2 thoughts on “Simple assign vs. php_array”