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
";$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
";$start = [gettimeofday()];
如果使用一个(,)较大的数据集和时间长度,则“完整”文件上的上述逻辑需要60至120秒才能执行。 是否有效率更高的方法实现同样的结果?
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.