in reply to Re^3: quick sort. How do it faster
in thread quick sort. How do it faster
I'm surprised that introducing that temporary variable for swaps makes such a difference, but it does appear to. Do you have any explanation?
Not really. Just that it seems to be some inefficiency inherent in the way list ops are implemented in perl.
Perhaps even more surprising is that -- for numeric data -- the old saw, triple xor-assignment is more efficient than both the list assignment and the introduced temporary:
cmpthese -1,{ a=>q[ my( $a, $b ) = ( 1, 2 ); ( $a, $b ) = ( $b, $a ) for 1. +.1000; ], b=>q[ my( $a, $b,$t ) = ( 1, 2 ); $t = $a, $a = $b, $b = $t for 1. +.1000; ], c=>q[ my( $a, $b ) = ( 1, 2 ); $a ^= $b ^= $a ^= $b for 1. +.1000; ] };; Rate a b c a 3253/s -- -34% -42% b 4913/s 51% -- -13% c 5632/s 73% 15% --
That really ought not be the case.
|
|---|