O () says nothing about how long the algorithm has been running. For example, all other things being equal, I always chose Algorithm 2 from the following two:
- Algorithm 1: O (2 * N + 1000 days) = O (N)
- Algorithm 2: O (5 * N + 100 ms) = O (N log N)
O () indicates how time the algorithm takes scales as the input size increases. (Well, it can be used for any resources, not just time.) Since the previous two answers only talk about O () terms, they are useless.
If you want to know how fast the algorithm, the algorithm of which is better for entering a given size, you will need to compare it.
In this case, it looks like List :: Util min
always significantly better.
$ perl x.pl 10 Rate sort LUmin sort 1438165/s -- -72% LUmin 5210584/s 262% -- $ perl x.pl 100 Rate sort LUmin sort 129073/s -- -91% LUmin 1485473/s 1051% -- $ perl x.pl 1000 Rate sort LUmin sort 6382/s -- -97% LUmin 199698/s 3029% --
the code:
use strict; use warnings; use Benchmark qw( cmpthese ); use List::Util qw( min ); my %tests = ( 'sort' => 'my $x = ( sort { $a <=> $b } @n )[0];', 'LUmin' => 'my $x = min @n;', ); $_ = 'use strict; use warnings; our @n; ' . $_ for values %tests; local our @n = map rand, 1..( $ARGV[0] // 10 ); cmpthese(-3, \%tests);
source share