Moose真的这么慢吗? [英] Is Moose really this slow?

查看:51
本文介绍了Moose真的这么慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了 Moose.通过实验,我在 Moose 中重写了一个现有模块.这似乎是避免编写大量重复代码的便捷方法.我运行了模块的测试,我注意到它有点延迟.我用 -d:DProf 对代码进行了分析,似乎只包含了该行

I recently downloaded Moose. Experimentally, I rewrote an existing module in Moose. It seems to be convenient way to avoid writing lots of repetitive code. I ran the tests of the module, and I noticed it was a bit delayed. I profiled the code with -d:DProf and it seems that just including the line

no Moose;

在代码中增加了大约 0.25 秒的运行时间(在我的电脑上).这是典型的吗?是我做错了什么,是我安装错了,还是我们真的应该期待这么长的延迟?

in the code increases the running time by about 0.25 seconds (on my computer). Is this typical? Am I doing something wrong, did I misinstall it, or should we really expect this much delay?

推荐答案

是的,使用 Moose 会有些不利.然而,这只是启动惩罚,而不是运行时;如果您正确地编写了所有内容,那么运行时的速度会非常快.

Yes, there's a bit of a penalty to using Moose. However, it's only a startup penalty, not at runtime; if you wrote everything properly, then things will be quite fast at runtime.

您是否也包括了这一行:

Did you also include this line:

__PACKAGE__->meta->make_immutable;

在您没有Moose;的所有课程中?调用此方法将使其(运行时)更快(以启动时间为代价).特别是,对象构造和销毁在您的类中有效地内联",并且不再调用元 API.强烈建议您使类不可变.它使您的代码更快,编译时成本很小.这在创建多个对象时尤其明显.12

in all your classes when you no Moose;? Calling this method will make it (runtime) faster (at the expense of startup time). In particular, object construction and destruction are effectively "inlined" in your class, and no longer invoke the meta API. It is strongly recommended that you make your classes immutable. It makes your code much faster, with a small compile-time cost. This will be especially noticeable when creating many objects.1 2

不过,有时候这个代价还是太大了.如果您在脚本中使用 Moose,或者在编译时间占总使用时间很大一部分的其他方式中使用 Moose,请尝试执行 s/Moose/Moo/g -- 如果您不这样做不使用 MooseX 模块,您可以切换到 Moo,其目标是更快(在启动时) 同时保留了 Moose 90% 的灵活性.

However, sometimes this cost is still too much. If you're using Moose inside a script, or in some other way where the compilation time is a significant fraction of your overall usage time, try doing s/Moose/Moo/g -- if you don't use MooseX modules, you can likely switch to Moo, whose goal is to be faster (at startup) while retaining 90% of the flexibility of Moose.

既然您正在使用 Web 应用程序,您是否考虑过使用 Plack/PSGI?

Since you are working with a web application, have you considered using Plack/PSGI?

1来自 make_immutable 的文档,在 Moose::Cookbook::Basics::Recipe7
2另见 Stevan Little 的文章:为什么推荐给 Moose 类使用 make_immutable

这篇关于Moose真的这么慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆