Skip to main content

What"s better to use in PHP $array[] = $value or array_push($array, $value)?



What's better to use in PHP for appending an array member $array[] = $value or array_push($array, $value) ?





Though the manual says you're better off to avoid a function call, I've also read $array[] is much slower than array_push() . Does anyone have any clarifications or benchmarks?





Thank you.



Source: Tips4all

Comments

  1. No benchmarks, but I personally feel like $array[] is cleaner to look at, and honestly splitting hairs over milliseconds is pretty irrelevant unless you plan on appending hundreds of thousands of strings to your array.

    Edit: Ran this code:

    $t = microtime(true);
    $array = array();
    for($i = 0; $i < 10000; $i++) {
    $array[] = $i;
    }
    print microtime(true) - $t;
    print '<br>';
    $t = microtime(true);
    $array = array();
    for($i = 0; $i < 10000; $i++) {
    array_push($array, $i);
    }
    print microtime(true) - $t;


    The first method using $array[] is almost 50% faster than the second one.

    Some benchmark results:

    Run 1
    0.0054171085357666 // array_push
    0.0028800964355469 // array[]

    Run 2
    0.0054559707641602 // array_push
    0.002892017364502 // array[]

    Run 3
    0.0055501461029053 // array_push
    0.0028610229492188 // array[]


    This shouldn't be surprising, as the PHP manual notes this:


    If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.


    The way it is phrased I wouldn't be surprised if array_push is more efficient when adding multiple values. EDIT: Out of curiosity, did some further testing, and even for a large amount of additions, individual $array[] calls are faster than one big array_push. Interesting.

    ReplyDelete
  2. Word on the street is that [] is faster because no overhead for the function call. Plus, no one really likes PHP's array functions...

    "Is it...haystack, needle....or is it needle haystack...ah, f*** it...[] = "

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex