Simple assign vs. php_array

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:

array_push: a tip

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.
  • Method:

    Test results

    Conclusion: Perform a code review changing array_push calls by simple assigns will give you few seconds faster. So do it right now ;-).

    Regards!

About Adriano Laranjeira

Software engineer & developer See more at http://about.me/arglbr
This entry was posted in English and tagged . Bookmark the permalink.

2 Responses to Simple assign vs. php_array

  1. Pingback: Adriano Laranjeira » Blog Archive » Atribuição simples x php_array

  2. Pingback: Adriano Laranjeira » Blog Archive » Gerador de planilhas Excel/Calc em PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s