Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Better way to do this

by Cristoforo (Curate)
on Feb 21, 2016 at 00:58 UTC ( [id://1155742]=note: print w/replies, xml ) Need Help??


in reply to Better way to do this

This script ran in less than 1 second. But it doesn't print out the subarrays which would make it faster. Still, I can't see why it is taking your script over an hour to run. Unless you are repeatedly constructing a 90_000 element array and the other arrays and hash.
#!/usr/bin/perl use strict; use warnings; my @unique = map int rand(45000), 1 .. 90_000; my %first_index = map { $unique[$_] => $_ } reverse 0 .. $#unique; my @in = map $unique[rand @unique], 1 .. 10000; my @idxs = sort {$a <=> $b} map $first_index{$_} // (), @in; my $first = $idxs[0]; my @sub_arrays; my $time = time; for (1 .. $#idxs) { my $diff = $idxs[$_] - $first ; if (($diff > 5) && ($diff < 10)) { push @sub_arrays, [ @unique[$first+1..$idxs[$_]-1] ]; } $first = $idxs[$_]; } #use Data::Dumper; print Dumper \@sub_arrays; print "Number of sub arrays: ", scalar @sub_arrays, "\n"; print "Time = ", time - $time, "\n";
This printed results for 1 run:
Number of sub arrays: 1330 Time = 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found