【问题标题】:How to get size of array in bytes in perl?如何在perl中获取数组的大小(以字节为单位)?
【发布时间】:2014-12-04 09:48:13
【问题描述】:

我需要在 perl 中获取数组的大小,但大小必须以字节为单位。我找不到任何功能。 提前致谢

【问题讨论】:

  • 这是一个非常模糊的问题。您是否只关注数组本身的内存中的常驻大小?数组及其值呢?如果这些值本身就是引用怎么办——你也计算它引用的数据吗?或者,如果您要以某种形式将其序列化,您可能更关注将输出的字节数?

标签: arrays perl function size byte


【解决方案1】:

您可以使用 Devel::Size 模块执行此操作 -> https://metacpan.org/pod/Devel::Size

#!/usr/bin/perl

use strict;
use warnings;
use Devel::Size qw(total_size);

my @arr = (1, 2, 3, "Foo", "Bar", "Baz", [4, 5, 6], {xyz => 2048});

print "Size: ", total_size(\@arr), " bytes.\n";

在我的系统上打印:

bria@hel:~$ ./size.pl
Size: 765 bytes.
bria@hel:~$

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 2013-02-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多