2005-12-11

When perl is not quite fast enough

来源: 本站收集整理 作者:佚名 评论 0 条
 exists is false, so the else block executes. It stores an arrayref containing the code page and character number in that page. That's three things per character, and there are a lot of characters in Chinese.

If it ever sees the same Unicode character again, it prints a warning message. The warning message is just a string, and this is the only place that uses the data in %seen. So I changed the code - I pre-formatted that bit of the error message, and stored a single scalar rather than the three:

    if (exists $seen{$uch}) {

        warn sprintf("UX is XX and X\n",

                     $val,$page,$ch,$seen{$uch});

    }

    else {

        $seen{$uch} = $page << 8 | $ch;

    }

That reduced the memory usage by a third, and it runs more quickly.

Step by step

How do you make things faster? Well, this is something of a black art, down to trial and error. I'll expand on aspects of these 4 points in the next slides.

哪些是慢的?
你必须找到那些使真正慢的东西。在已足够快的代码上浪费精力是不值得的 - 将其放在能获得最大回报的代码上吧。
考虑重写
不管你怎么咒骂它们,并非所有的代码都能变得快起来。所以剩下真正能加速的是找出另一种能完成同样工作的方法。
尝试
但是这或许不可行。检查它是更快的和给出了同样的结果。
Note results
Either way, note your results - I find a comment in the code is good. It's important if an idea didn't work, because it stops you or anyone else going back and trying the same thing again. And it's important if a change does work, as it stops someone else (such as yourself next month) tidying up an important optimisation and losing you that hard won speed gain.

By having commented out slower code near the faster code you can look back and get ideas for other places you might optimise in the same way.

简单的小事

下面是一些我认为有用的习惯,所以您应当将它们运用到日常程序中。

AutoSplitAutoLoader
假如您在写模块,使用模块 AutoSplitAutoLoader 可以使 perl 只加载那些特定程序实际所用到的模块部分。如此您能获得两个好处 - 在开始加载时不浪费 CPU 去倒入您不用的模块部分,还有当代码编译时不浪费 RAM 去保持着编译的结构。所以您的模块能加载更快,和使用更少的 RAM 。

一个可能的问题是使用 AutoLoader 让子程序带来调试混乱。当处于测试状态,您能通过在自动加载子程序前添加注释 __END__ 来使 AutoLoader 失效。如此一来,它们就普通地被加载,编译和测试了。

  ...

  1;

  # While debugging, disable AutoLoader like this:

  # __END__

  ...

当然,为了使 use 正常,您还得在加载程序段的后面另添加 1; 和可能要另一个 __END__
共13页: 上一页 [1] [2] [3] 4 [5] [6] [7] [8] [9] [10] [11] [12] [13] 下一页

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

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