use strict;
use Time::HiRes qw[gettimeofday tv_interval];
my $start_index = int(rand(50))+100;#this value is arbitrary for this discussion
my $duration = 75;#also arbitrary but assume it will always be several times the size of the dataset
my $hash = {};
my @dataset = qw(foo bar baz qux bob joe sue tom);
my $partial = $duration % scalar(@dataset);
my $full = ($duration - $partial) / scalar(@dataset);
my $start = [gettimeofday()];
for my $index (0..$#dataset) {
my $w = $dataset[$index];
for (0..$full-1) {
my $i = $start_index + $index + (scalar(@dataset) * $_);
$hash->{$i} = $w;
}
}
print " full ".tv_interval($start)." secs\n";$start = [gettimeofday()];
for my $index (0..$partial-1) {
my $w = $dataset[$index];
my $s = $start_index + $index + (scalar(@dataset) * $full);
$hash->{$s} = $w;
}
print " part ".tv_interval($start)." secs\n";$start = [gettimeofday()];
When implemented with a (much) larger dataset and duration, the above logic in the “full” loop takes 60~120 seconds to execute. Is there a more efficient method of achieving the same results?
Edit:
To give more perspective as to the size of the dataset this is used in, this performance optimization is for a signal processing program.
here’s the solution: