2005-12-11

When perl is not quite fast enough

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

yay for y

y, or tr, is the transliteration operator. It's not as powerful as the general purpose regular expression engine, but for the things it can do it is often faster.

tr/!// # 计算字符次数的更快的方法
tr 并不删除字符除非你使用了 /d 。假如你没有任何代替字符操作,它使用了只读。在标量上下文中,它返回匹配字符的次数。这是一种最快速的方法来计算单字符或字符串被匹配的次数。(它比在列表上下文上使用 m/.../g 要快。但是假如你只是检查某字符是否出现一次以上,那请使用 m/.../ ,因为它会在第一次检查到就停止,而 tr/// 会检查到最后)
tr/q/Q/ 比 s/q/Q/g 更快
tr is also faster than the regexp engine for doing character-for-character substitutions.
tr/a-z//d 比 s/[a-z]//g 更快
tr is faster than the regexp engines for doing character range deletions. [When writing the slide I assumed that it would be faster for single character deletions, but I Benchmarked things and found that s///g was faster for them. So never guess timings; always test things. You'll be surprised, but that's better than being wrong]

Ops are bad, m'kay

Another example lifted straight from enc2xs of something that I managed to accelerate quite a bit by reducing the number of ops run. The code takes a scalar, and prints out each byte as \x followed by 2 digits of hex, as it's generating C source code:

    #foreach my $c (split(//,$out_bytes)) {

    #  $s .= sprintf "\\xX",ord($c);

    #}

    # 9.5% faster changing that loop to this:

    $s .= sprintf  ("\\xX" x length $out_bytes), unpack "C*", $out_bytes;

The original makes a temporary list with split [not bad in itself - ops are more important than CPU or RAM] and then loops over it. Each time round the loop it executes several ops, including using ord to convert the byte to its numeric value, and then using sprintf with the format "\\xX" to convert that number to the C source.

The new code effectively merges the split and looped ord into one op, using unpack's C format to generate the list of numeric values directly. The more interesting (arguably sick) part is the format to sprintf, which is inside (...). You can see from the .=
共13页: 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 12 [13] 下一页

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

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