Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Fast way to check if a sort was doen

by lima1 (Curate)
on Jun 28, 2007 at 16:20 UTC ( [id://623934]=note: print w/replies, xml ) Need Help??


in reply to Fast way to check if a sort was doen

Do you have two arrays, before and after sorting (yeah, I read that you don't want to compare the arrays...)? If yes, then for big arrays, you could write a simple test that compares n indices:
for my $r (@rand) { return 0 if $a[$r] ne $b[$r]; } return 1;
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.)

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
    On my machine it's the opposite. Join is faster. I tried several array sizes (sorted and unsorted arrays) with random numbers. I have Windows laptop with Activestate Perl build 819 (v5.8.8).
    Benchmark: timing 1000 iterations of join, md5... join: 46 wallclock secs (43.51 usr + 0.07 sys = 43.58 CPU) @ 22 +.95/s (n=1000) md5: 55 wallclock secs (53.15 usr + 0.03 sys = 53.18 CPU) @ 18 +.81/s (n=1000)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://623934]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-23 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found