2005-12-11

When perl is not quite fast enough

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

    LISTOP (0x16d9c8) leave [1]

        OP (0x16d9f0) enter

        COP (0x16d988) nextstate

        LOGOP (0x16d940) mapwhile [2]

            LISTOP (0x16d8f8) mapstart

                OP (0x16d920) pushmark

                UNOP (0x16d968) null

                    UNOP (0x16d7e0) null

                        LISTOP (0x115370) scope

                            OP (0x16bb40) null [174]

                            UNOP (0x16d6e0) hex [1]

                                UNOP (0x16d6c0) null [15]

                                    SVOP (0x10e6b8) gvsv  GV (0xf4224) *_

                PMOP (0x114b28) match /(....)/

                    UNOP (0x16d7b0) null [15]

                        SVOP (0x16d700) gvsv  GV (0x111f10) *line

At the bottom you can see how the match /(....)/ is just one op. But the next diagonal line of ops from mapwhile down to the match are all the ops that make up the map. Lots of them. And they get run each time round map's loop. [Note also that the {}s mean that map enters scope each time round the loop. That not a trivially cheap op either]

Whereas my replacement code looks like this:

$ perl -MO=Terse -e'unpack "n*", pack "H*", $line;'

    LISTOP (0x16d818) leave [1]

        OP (0x16d840) enter

        COP (0x16bb40) nextstate

        LISTOP (0x16d7d0) unpack

            OP (0x16d7f8) null [3]

            SVOP (0x10e6b8) const  PV (0x111f94) "n*"

            LISTOP (0x115370) pack [1]

                OP (0x16d7b0) pushmark

                SVOP (0x16d6c0) const  PV (0x111f10) "H*"

                UNOP (0x16d790) null [15]

                    SVOP (0x16d6e0) gvsv  GV (0x111f34) *line

There are less ops in total. And no loops, so all the ops you see execute only once. :-)

[My helpful hecklers pointed out that it's hard to work out what an op is. Good call. There's roughly one op per symbol (function, operator, variable name, and any other bit of perl syntax). So if you golf down the number of functions and operators your program runs, then you'll be reducing the number of ops.]

[These were supposed to be the bonus slides. I talked to fast (quelle surprise) and so manage to actually get through the lot with time for questions]

Memoize

Caches function results
MJD's Memoize follows the grand perl tradition by trading memory for speed. You tell Memoize the name(s) of functions you'd like to speed up, and it does symbol table games to transparently intercept calls to them. It looks at the parameters the function was called with, and uses them to decide what to do next. If it hasn't seen a particular set of parameters before, it calls the original function with the parameters. However, before returning the result, it stores it in a hash for that function, keyed by the function's parameters. If it has seen the parameters before, then it just returns the result direct from the hash, without even bothering to call the function.
共13页: 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] 10 [11] [12] [13] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:被百度降权后,网站该怎么办?  
下一篇:SQL Server2000数据库文件损坏时如何恢复
    评论加载中…
 推荐文章
     

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