in reply to Fast way to check if a sort was doen
Not 100% safe of course, but if the input lines are not related, it should work. To be sure, you could apply a second test if the first test returned 1: Array::Compare uses join to compare two arrays. (This is probably fast enough even without the first test.)for my $r (@rand) { return 0 if $a[$r] ne $b[$r]; } return 1;
If you don't have the two arrays, you won't get better than O(n) or you use a similar (unsafe) test like above - check if n blocks of size 2 are sorted.
UPDATE: md5 seems to be a little bit faster than join:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all) ; use List::Util qw(shuffle); use Digest::MD5 qw(md5); open my $JOHN, '<', 'dictionary' or die $!; my @array = <$JOHN>; my @sorted = sort @array; $a = \@array; $b = \@sorted; timethese(5000, { 'join' => sub { return join('|',@$a) eq join('|', @$b); }, 'md5' => sub { return Digest::MD5::md5(@$a) eq Digest::MD5::md5(@$b) +; }, });
perl cmparray.pl Benchmark: timing 5000 iterations of join, md5... join: 29 wallclock secs (27.91 usr + 0.18 sys = 28.09 CPU) @ 17 +8.00/s (n=5000) md5: 27 wallclock secs (25.20 usr + 0.19 sys = 25.39 CPU) @ 19 +6.93/s (n=5000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fast way to check if a sort was doen
by aku (Monk) on Jun 28, 2007 at 21:37 UTC |