2005-12-11

When perl is not quite fast enough

来源: 本站收集整理 作者:佚名 评论 0 条
 
    0:      0000DEAD        0000BEEF

    1:      80006F56        00017DDE

    2:      400037AB        0002FBBC

    3:      A0001BD5        0005F778

    4:      D0000DEA        000BEEF0

    ...
编译您自己的 perl ?
您是否用系统提供的 perl 运行程序。编译自己的 perl 能让程序跑得更快。例如,当线程编译进 perl ,所有它的内部变量都要满足线程安全/thread safe,这能让程序变慢一点。假如 perl 可以使用线程,但你不使用线程,于是你毫无理由地付出了一点速度。同样地,你能有个比系统使用的更好的编译器。例如,我发现使用 gcc 3.2 比 2.9.5 某些我的 C 代码快了 5% 。[某个对我有益的激烈质问者/hecklers说他提升了 14% ,(假如我记忆无误的话)这是从重编译 perl 编译器所获得的]
不同的 perl 版本?
试着采用不同的 perl 版本。不同的版本对某些事情有提速。假如你使用了一个旧的 perl ,试着用最新版本。假如你运行最新版本而没使用这些最新的特性,那试着使用一个旧版本。

放逐愚蠢的魔鬼/Banish the demons of stupidity

您是否使用了这种语言最棒的特性?

hashes
引用 Larry Wall 的话 - 在关联数组中使用线性搜索就像试着联合某人对付 loaded Uzi 。

我相信你不会这么做。但是你是否保留您的数组已排序,这样你能使用对分检索?这很快。但是使用 hash 应该会更快。

regexps
没有正则表达式/regexps的语言需要你另外写代码来解析字符串。perl 有 regexps ,又它们重写或许能获得 10 倍加速。甚至使用锚标\G和标记/gc可能会更快。
    if ( /\G.../gc ) {

        ...

    } elsif ( /\G.../gc ) {

        ...

    } elsif ( /\G.../gc ) {
packunpack
pack 和 unpack 有太多的特性值得去记住。翻看 manpage - 你可能只用一个 unpack 代替整个子程序。
undef
undef. 什么是我意味的 undef?

您是否只为了丢弃它而计算?

For example the script in the Encode module that compiles character conversion tables would print out a warning if it saw the same character twice. If you or I build perl we'll just let those build warnings scroll off the screen - we don't care - we can't do anything about it. And it turned out that keeping track of everything needed to generate those warnings was slowing things down considerably. So I added a flag to disable that code, and perl 5.8 defaults to use it, so it builds more quickly.

间断

许多对我有益的激烈质问者/hecklers (大部分是看过讲演的 London.pm (我将 David Adler 做为 London.pm 的一分子因为他订阅了这份列表))希望我提醒各位除非你确实绝对的需要,否则真的不应该去优化。您在让您的代码更难维修,更难扩展和更易引进新的 bugs 。很可能在一开始你就在需要优化的地方做了些不该做的事。

我同意上述观点。

同样,我也不想改变幻灯片的运行顺序。There isn't a good order to try to describe things in, and some of the ideas that follow are actually more "good practice" than optimisation techniques, so possibly ought to come before the slides on finding slowness. I'll mark what I think are good habits to get into, and once you understand the techniques then I'd hope that you'd use them automatically when you first write code. That way (hopefully) your code will never be so slow that you actually want to do some of the brute force optimising I describe here.
共13页: 上一页 [1] 2 [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:被百度降权后,网站该怎么办?  
下一篇:SQL Server2000数据库文件损坏时如何恢复
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号