Third on the list is encode_U, which with 45,000 calls is similar, and worth looking at. [Actually, it was trivial code and in the real enc2xs I inlined it]
utf8::unicode_to_native and utf8::encode are built-ins, so you won't be able to change that.
Don't bother below there, as you've accounted for 90% of total program time, so even if you did a perfect job on everything else, you could only make the program run 10% faster.
compile_ucm is trickier - it's only called 6 times, so it's not obvious where to look for what's slow. Maybe there's a loop with many iterations. But now you're guessing, which isn't good.
One trick is to break it into several subroutines, just for benchmarking, so that DProf gives you times for different bits. That way you can see where the juicy bits to optimise are.
Devel::SmallProf should do line by line profiling, but every time I use it it seems to crash.
现在您已经确定了慢的地方,您还需要在两个代码中找到更快的一个。模块 Benchmark 能让这变得轻易。一个非凡好的子程序是 cmpthese ,它能摘录代码片段而且绘制图表。cmpthese 可于 perl 5.6 的 Benchmark 里发现。
因为为了比较各运行 1000 次的两个代码片段 orig 和 new ,您可以这么做
use Benchmark ':all';
sub orig {
...
}
sub new {
...
}
cmpthese (10000, { orig => \&orig, new => \&new } );Benchmark 运行了两者,分别计时,然后输出一个有用的对比图表:
Benchmark: timing 10000 iterations of new, orig...
new: 1 wallclock secs ( 0.70 usr 0.00 sys = 0.70 CPU) @ 14222.22/s (n=10000)
orig: 4 wallclock secs ( 3.94 usr 0.00 sys = 3.94 CPU) @ 2539.68/s (n=10000)
Rate orig new
orig 2540/s -- -82%
new 14222/s 460% --很轻易看清楚新的代码比原始代码快 4 倍。
Actually, I didn't tell the whole truth earlier about what causes slowness in perl. [And astute hecklers such as Philip Newton had already told me this]
When perl compilers your program it breaks it down into a sequence of operations it must perform, which are usually referred to as ops. So when you ask perl to compute $a = $b $c it actually breaks it down into these ops:
$b onto the stack $c onto the stack
评论加载中…
![]() |