gpt4 book ai didi

perl - 在 perl 中。 hash是如何在内存中存储数据的

转载 作者:行者123 更新时间:2023-11-30 23:59:51 25 4
gpt4 key购买 nike

我有一个很大的 xml 文件,解析它会消耗大量内存。
因为我相信大部分是由于文件中有很多用户名。
我将每个用户名的长度从 ~28 字节更改为 10 字节。
并再次运行。但它仍然需要几乎相同数量的内存。
到目前为止,xml 文件是用 SAX 解析的,在处理过程中,结果是
存储在哈希结构中,如下所示:$this->{'date'}->{'school 1'}->{$class}->{$student}...
为什么我把学生名字的长度缩短后内存仍然如此之多?是否可以
当数据存储在哈希内存中时。无论字符串的长度有多长,都会有很多开销?

最佳答案

Perl 哈希使用一种称为桶链的技术。所有具有相同散列的键(参见 PERL_HASH_INTERNAL 中的宏 hv.h )进入同一个“桶”,一个线性列表。

根据perldata文件

If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's internal hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16" , which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen. If a tied hash is evaluated in scalar context, a fatal error will result, since this bucket usage information is currently not available for tied hashes.



要查看您的数据集是否具有病理分布,您可以检查标量上下文中的各个级别,例如,
print scalar(%$this), "\n",
scalar(%{ $this->{date} }), "\n",
scalar(%{ $this->{date}{"school 1"} }), "\n",
...

有关过时的概述,请参阅 How Hashes Really Work在 perl.com。

学生姓名长度的适度减少,即向下四级的键,不会产生显着差异。通常,perl 实现强烈倾向于将内存用于解决问题。这不是你父亲的 FORTRAN。

关于perl - 在 perl 中。 hash是如何在内存中存储数据的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com