- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Moose 对象似乎是带有成员变量作为条目的祝福 hashrefs。使用哈希语法访问成员是否安全?我想要的只是获取/设置成员变量的简单能力,但我发现 perl 生成的访问器的速度对于我的应用程序来说是 Not Acceptable ,以至于我考虑在非 moose OO 中重写它。是的,我知道优化是一种可处以死刑的罪行,但我有十亿多行文件,添加一个简单的行计数器变量会增加十亿多函数调用,这会在单次运行中增加数小时。
显示缓慢的示例 (100x):
package Bullwinkle;
use strict;
use warnings;
use Moose;
has 'x' => (is => 'rw', default => 0);
# counter is also slow
has 'counter' => (
traits => ['Counter'],
is => 'ro',
isa => 'Num',
default => 0,
handles => {
inc_counter => 'inc',
},
);
package main;
use strict;
use warnings;
use Benchmark qw/timethese cmpthese/;
my $bull = Bullwinkle->new();
my $hash = { x => 0 };
cmpthese(-1,{
bull => sub{
$bull->x(1 + $bull->x());
},
bull_direct => sub{
++$bull->{x}
},
bull_counter => sub{
$bull->inc_counter();
},
hash => sub{
++$hash->{x}
},
});
最佳答案
我会说打破封装永远是不安全的,Moose 与否。
请注意,您的 bull
测试不会慢 100 倍。请记住,它使用了两次访问器。就在信封的背面,它从每秒 1,000,000 次运行到每秒 2,000,000 次访问。与散列相比,它大约是 6 倍。但是,加上一堆这些减速,它可以加起来。
但是,我不认为 Moose 是那里的问题。当我用一个做同样事情的非 Moose 类尝试它时,结果并没有好多少。看起来大部分减速只是来自正常的面向对象的方法调用。使用 inc_counter
的非 Moose 类和 Moose 类之间的差异约为 2 倍。
基准测试是非常棘手的事情。您确实需要努力梳理出不同的部分,并确保您测试适当的基本案例。当你得到疯狂的数字时,你应该从怀疑论者的角度开始调整,直到它们不再那么疯狂。例如,即使 Moose 很慢,如果它像您猜测的那样慢,我认为没有人会使用它。慢一点是一回事,数量级或数量级是另一回事。
我也对基准测试很好奇,我想用 make_immutable
尝试一下。这是我在运行 Lion 的 Mac Pro 上使用 Moose-2.0403 的默认 Perl5.14.2 的结果。 Bull 是你的原始代码,Rocky 使用 make_immutable
,而 Natasha 是一个非 Moose 类,它做同样的事情:
Rate bull rocky natasha bull_counter rocky_counter natasha_x rocky_x bull_x natasha_counter rocky_direct bull_direct natasha_direct hash
bull 728177/s -- -6% -17% -42% -43% -65% -66% -67% -71% -93% -94% -94% -95%
rocky 771011/s 6% -- -12% -39% -39% -63% -64% -65% -70% -93% -93% -94% -95%
natasha 877713/s 21% 14% -- -30% -31% -58% -59% -60% -66% -92% -92% -93% -94%
bull_counter 1260308/s 73% 63% 44% -- -1% -40% -42% -42% -51% -88% -89% -89% -91%
rocky_counter 1274310/s 75% 65% 45% 1% -- -39% -41% -42% -50% -88% -89% -89% -91%
natasha_x 2105717/s 189% 173% 140% 67% 65% -- -3% -4% -17% -81% -81% -82% -85%
rocky_x 2163925/s 197% 181% 147% 72% 70% 3% -- -1% -15% -80% -81% -82% -85%
bull_x 2184533/s 200% 183% 149% 73% 71% 4% 1% -- -14% -80% -80% -82% -85%
natasha_counter 2548621/s 250% 231% 190% 102% 100% 21% 18% 17% -- -77% -77% -79% -82%
rocky_direct 10901037/s 1397% 1314% 1142% 765% 755% 418% 404% 399% 328% -- -3% -9% -24%
bull_direct 11202734/s 1438% 1353% 1176% 789% 779% 432% 418% 413% 340% 3% -- -6% -21%
natasha_direct 11939231/s 1540% 1449% 1260% 847% 837% 467% 452% 447% 368% 10% 7% -- -16%
hash 14252488/s 1857% 1749% 1524% 1031% 1018% 577% 559% 552% 459% 31% 27% 19% --
#!/Users/brian/bin/perls/perl5.14.2
use v5.10.1;
use strict;
use warnings;
use Benchmark qw/timethese cmpthese/;
package Bullwinkle {
use strict;
use warnings;
use Moose;
has 'x' => (is => 'rw', default => 0);
# counter is also slow
has 'counter' => (
traits => ['Counter'],
is => 'ro',
isa => 'Num',
default => 0,
handles => {
inc_counter => 'inc',
},
);
}
package Rocky {
use strict;
use warnings;
use Moose;
has 'x' => (is => 'rw', default => 0);
# counter is also slow
has 'counter' => (
traits => ['Counter'],
is => 'ro',
isa => 'Num',
default => 0,
handles => {
inc_counter => 'inc',
},
);
__PACKAGE__->meta->make_immutable;
}
package Natasha {
use strict;
use warnings;
sub new { bless { 'x' => 0 }, $_[0] }
sub inc_counter { $_[0]->{x} += 1 }
sub x {
if( defined $_[1] ) { $_[0]->{x} = $_[1] }
else { $_[0]->{x} }
}
}
my $bull = Bullwinkle->new;
my $rocky = Rocky->new;
my $natasha = Natasha->new;
my $hash = { 'x' => 0 };
cmpthese(-1,{
bull => sub { $bull->x(1 + $bull->x ) },
bull_x => sub { $bull->x },
bull_direct => sub { ++$bull->{'x'} },
bull_counter => sub { $bull->inc_counter },
rocky => sub { $rocky->x(1 + $rocky->x ) },
rocky_x => sub { $rocky->x },
rocky_direct => sub { ++$rocky->{'x'} },
rocky_counter => sub { $rocky->inc_counter },
natasha => sub { $natasha->x(1 + $natasha->x ) },
natasha_x => sub { $natasha->x },
natasha_direct => sub { ++$natasha->{'x'} },
natasha_counter => sub { $natasha->inc_counter },
hash => sub { ++$hash->{'x'} },
});
关于perl - 在 Moose 中,通过对象的内部哈希直接访问成员变量是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102856/
我正在尝试 grep conf 文件中所有不以 开头的有效行 哈希(或) 任意数量的空格(0 个或多个)和一个散列 下面的正则表达式似乎不起作用。 grep ^[^[[:blank:]]*#] /op
我正在使用哈希通过 URL 发送 protected 电子邮件以激活帐户 Hash::make($data["email"]); 但是哈希结果是 %242y%2410%24xaiB/eO6knk8sL
我是 Perl 的新手,正在尝试从文本文件创建散列。我有一个代码外部的文本文件,旨在供其他人编辑。前提是他们应该熟悉 Perl 并且知道在哪里编辑。文本文件本质上包含几个散列的散列,具有正确的语法、缩
我一直在阅读 perl 文档,但我不太了解哈希。我正在尝试查找哈希键是否存在,如果存在,则比较其值。让我感到困惑的是,我的搜索结果表明您可以通过 if (exists $files{$key}) 找到
我遇到了数字对映射到其他数字对的问题。例如,(1,2)->(12,97)。有些对可能映射到多个其他对,所以我真正需要的是将一对映射到列表列表的能力,例如 (1,2)->((12,97),(4,1))。
我见过的所有 Mustache 文档和示例都展示了如何使用散列来填充模板。我有兴趣去另一个方向。 EG,如果我有这个: Hello {{name}} mustache 能否生成这个(伪代码): tag
我正在尝试使用此公式创建密码摘要以获取以下变量,但我的代码不匹配。不确定我做错了什么,但当我需要帮助时我会承认。希望有人在那里可以提供帮助。 文档中的公式:Base64(SHA1(NONCE + TI
我希望遍历我传递给定路径的这些数据结构(基本上是目录结构)。 目标是列出根/基本路径,然后列出所有子 path s 如果它们存在并且对于每个子 path存在,列出 file从那个子路径。 我知道这可能
我希望有一个包含对子函数的引用的散列,我可以在其中根据用户定义的变量调用这些函数,我将尝试给出我正在尝试做的事情的简化示例。 my %colors = ( vim => setup_vim()
我注意到,在使用 vim 将它们复制粘贴到文件中后尝试生成一些散列时,散列不是它应该的样子。打开和写出文件时相同。与 nano 的行为相同,所以一定有我遗漏的地方。 $ echo -n "foo"
数组和散列作为状态变量存在限制。从 Perl 5.10 开始,我们无法在列表上下文中初始化它们: 所以 state @array = qw(a b c); #Error! 为什么会这样?为什么这是不允
在端口 80 上使用 varnish 5.1 的多网站设置中,我不想缓存所有域。 这在 vcl_recv 中很容易完成。 if ( req.http.Host == "cache.this.domai
基本上,缓存破坏文件上的哈希不会更新。 class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage): pa
eclipse dart插件在“变量” View 中显示如下内容: 在“值”列中可见的“id”是什么意思? “id”是唯一的吗?在调试期间,如何确定两个实例是否相同?我是否需要在所有类中重写toStr
如何将Powershell中的命令行参数读入数组?就像是 myprogram -file file1 -file file2 -file file3 然后我有一个数组 [file1,file2,fil
我正尝试在 coldfusion 中为我们的安全支付网关创建哈希密码以接受交易。 很遗憾,支付网关拒绝接受我生成的哈希值。 表单发送交易的所有元素,并发送基于五个不同字段生成的哈希值。 在 PHP 中
例如,我有一个包含 5 个元素的哈希: my_hash = {a: 'qwe', b: 'zcx', c: 'dss', d: 'ccc', e: 'www' } 我的目标是每次循环哈希时都返回,但没
我在这里看到了令人作呕的类似问题,但没有一个能具体回答我自己的问题。 我正在尝试以编程方式创建哈希的哈希。我的问题代码如下: my %this_hash = (); if ($user_hash{$u
我正尝试在 coldfusion 中为我们的安全支付网关创建哈希密码以接受交易。 很遗憾,支付网关拒绝接受我生成的哈希值。 表单发送交易的所有元素,并发送基于五个不同字段生成的哈希值。 在 PHP 中
这个问题已经有答案了: Java - how to convert letters in a string to a number? (9 个回答) 已关闭 7 年前。 我需要一种简短的方法将字符串转
我是一名优秀的程序员,十分优秀!