Which is faster? Comparison or assignment?

前端 未结 12 1186
抹茶落季
抹茶落季 2020-12-05 04:16

I\'m doing a bit of coding, where I have to write this sort of code:

if( array[i]==false )
    array[i]=true;

I wonder if it should be re-w

相关标签:
12条回答
  • 2020-12-05 04:29

    Depends on the language. However looping through arrays can be costly as well. If the array is in consecutive memory, the fastest is to write 1 bits (255s) across the entire array with memcpy assuming your language/compiler can do this.

    Thus performing 0 reads-1 write total, no reading/writing the loop variable/array variable (2 reads/2 writes each loop) several hundred times.

    0 讨论(0)
  • 2020-12-05 04:38

    I believe if comparison and assignment statements are both atomic(ie one processor instruction) and the loop executes n times, then in the worst-case comparing then assigning would require n+1(comparing on every iteration plus setting the assignement) executions whereas constantly asssigning the bool would require n executions. Therefore the second one is more efficient.

    0 讨论(0)
  • 2020-12-05 04:38

    It all depends on the data type. Assigning booleans is faster than first comparing them. But that may not be true for larger value-based datatypes.

    0 讨论(0)
  • 2020-12-05 04:38

    If you just want to flip the values, then do:

    array[i] = !array[i];
    

    Performance using this is actually worse though, as instead of only having to do a single check for a true false value then setting, it checks twice.

    If you declare a 1000000 element array of true,false, true,false pattern comparision is slower. (var b = !b) essentially does a check twice instead of once

    0 讨论(0)
  • 2020-12-05 04:40

    This isn't just premature optimization, this is micro-optimization, which is an irrelevant distraction.

    Assuming your array is of boolean type then your comparison is unnecessary, which is the only relevant observation.

    0 讨论(0)
  • 2020-12-05 04:41

    Edit: I wrote a script in PHP. I just noticed that there was a glaring error in it meaning the best-case runtime was being calculated incorrectly (scary that nobody else noticed!)

    Best case just beats outright assignment but worst case is a lot worse than plain assignment. Assignment is likely fastest in terms of real-world data.

    Output:

    • assignment in 0.0119960308075 seconds
    • worst case comparison in 0.0188510417938 seconds
    • best case comparison in 0.0116770267487 seconds

    Code:

    <?php
    $arr = array();
    
    $mtime = explode(" ", microtime());
    $starttime = $mtime[1] + $mtime[0];
    
    reset_arr($arr);
    
    for ($i=0;$i<10000;$i++)
        $arr[i] = true;
    
    
    $mtime = explode(" ", microtime());
    $firsttime = $mtime[1] + $mtime[0];
    $totaltime = ($firsttime - $starttime);
    echo "assignment in ".$totaltime." seconds<br />"; 
    
    reset_arr($arr);
    
    for ($i=0;$i<10000;$i++)
        if ($arr[i])
            $arr[i] = true;
    
    $mtime = explode(" ", microtime());
    $secondtime = $mtime[1] + $mtime[0];
    $totaltime = ($secondtime - $firsttime);
    echo "worst case comparison in ".$totaltime." seconds<br />"; 
    
    reset_arr($arr);
    
    for ($i=0;$i<10000;$i++)
        if (!$arr[i])
            $arr[i] = false;
    
    $mtime = explode(" ", microtime());
    $thirdtime = $mtime[1] + $mtime[0];
    $totaltime = ($thirdtime - $secondtime);
    echo "best case comparison in ".$totaltime." seconds<br />"; 
    
    function reset_arr($arr) {
        for ($i=0;$i<10000;$i++)
            $arr[$i] = false;
    }
    
    0 讨论(0)
提交回复
热议问题