Skip to main content

array_unique vs array_flip


If I had an array of signed integers e.g:




Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)



To get unique values I would instinctively use array_unique but after consideration I could perform array_flip twice which would have the same effect, and I think it would be quicker?



array_unique O(n log n) because of the sort operation it uses



array_flip O(n)



Am I correct in my assumptions?



UPDATE / EXAMPLE:




$intArray1 = array(-4,1,2,3);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);

Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)
Array
(
[-3] => 0
[1] => 1
[2] => 2
[3] => 4
)
Array
(
[0] => -3
[1] => 1
[2] => 2
[4] => 3
)


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I benchmarked it for you: CodePad

    Your intuition on this was correct!

    $test=array();
    for($run=0; $run<1000; $run++)
    $test[]=rand(0,100);

    $time=microtime(true);

    for($run=0; $run<100; $run++)
    $out=array_unique($test);

    $time=microtime(true)-$time;
    echo 'Array Unique: '.$time."\n";

    $time=microtime(true);

    for($run=0; $run<100; $run++)
    $out=array_keys(array_flip($test));

    $time=microtime(true)-$time;
    echo 'Keys Flip: '.$time."\n";

    $time=microtime(true);

    for($run=0; $run<100; $run++)
    $out=array_flip(array_flip($test));

    $time=microtime(true)-$time;
    echo 'Flip Flip: '.$time."\n";


    Output:

    Array Unique: 1.1829199790955
    Keys Flip: 0.0084578990936279
    Flip Flip: 0.0083951950073242


    Note that array_keys(array_flip($array)) will give a new key values in order, which in many cases may be what you want (identical except much faster to array_values(array_unique($array))), whereas array_flip(array_flip($array)) is identical (except much faster) to array_unique($array) where the keys remain the same.

    ReplyDelete
  2. Nothing is better than running your own benchmark.

    ➜ 8321620 cat first.php
    <?php

    $arr = array(-3, 1, 2, 3, 3);

    for($i = 0; $i <= 1000000; $i++) {
    array_unique($arr);
    }
    ➜ 8321620 time php first.php
    php first.php 3.24s user 0.01s system 99% cpu 3.251 total
    ➜ 8321620 cat second.php
    <?php

    $arr = array(-3, 1, 2, 3, 3);

    for($i = 0; $i <= 1000000; $i++) {
    array_flip(array_flip($arr));
    }
    ➜ 8321620 time php second.php
    php second.php 1.50s user 0.01s system 99% cpu 1.514 total


    Update: Array with 1000 elements.

    ➜ 8321620 cat first.php
    <?php

    $arr = array();
    for($i = 0; $i <= 1000; $i++) {
    $arr[] = rand(0, 1000);
    }

    for($i = 0; $i <= 10000; $i++) {
    array_unique($arr);
    }
    ➜ 8321620 time php first.php
    php first.php 27.50s user 0.03s system 99% cpu 27.534 total
    ➜ 8321620 cat second.php
    <?php

    $arr = array();
    for($i = 0; $i <= 1000; $i++) {
    $arr[] = rand(0, 1000);
    }

    for($i = 0; $i <= 10000; $i++) {
    array_flip(array_flip($arr));
    }
    ➜ 8321620 time php second.php
    php second.php 1.59s user 0.01s system 99% cpu 1.604 total


    So yes, your assumption was correct.

    ReplyDelete
  3. you would have to use

    array_keys( array_flip( $array ) );


    which would take more time

    I would go for array_unique. It has the added benefit of explaining whats happening.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.