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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.