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