我正在编写一个生成大型数组散列(HoA)数据结构的脚本。我正在尝试优化我的脚本,因为目前它需要永远运行。
最近我从一个网站上读到,可以进一步提高脚本执行速度的一种方法是使用子例程将值存储在数组哈希中。它说它对大型数据结构特别有用。
这是文章给出的例子。
sub build_hash{
# takes 3 params: hashref, name, and value
return if not $_[2];
push(@{ $_[0]->{'names'} }, $_[1]);
push(@{ $_[0]->{'value'} }, $_[2]);
# return a reference to the hash (smaller than making copy)
return $_[0];
}
文章说调用这个子例程来构建 HoA比将值推送到子例程之外的数组散列上快 40% 。该子例程据说速度很快,因为它使用引用构建数据结构。
我想通过构建我自己的 HoA 来测试这个子程序。假设我想创建以下数组散列。
%HoA = (
'C1' => ['1', '3', '3', '3'],
'C2' => ['3','2'],
'C3' => ['1','3','3','4','5','5'],
'C4' => ['3','3','4'],
'C5' => ['1'],
);
我将如何实现 build_hash 来做到这一点?另外,我该如何调用这个子程序?
这就是我所拥有的,但它不是很有效。
# let AoA be an array of arrays that contains the values I want to assign
# to each key in %HoA
my @AoA = (
['1', '3', '3', '3'],
['3','2'],
['1','3','3','4','5','5'],
['3','3','4'],
['1']
);
my %HoA;
my $count = 1;
foreach my $ref (@AoA){
build_hash(\%HoA, $ref, "C$count");
$count++;
}
sub build_hash {
# takes 3 params: hashref, arrayref, and key
return if not $_[2];
push(@{ $_[0]->{$_[1]} }, $_[2]);
# return reference to HoA_ref (smaller than making copy)
return $_[0];
}