# 配置文件代码并将数据库写入 ./nytprof.out
perl -d:NYTProf some_perl.pl
# 将数据库转换为一组 html 文件,例如 ./nytprof/index.html
# 并在 nytprof/index.html 文件上打开 Web 浏览器
nytprofhtml --打开
Try starting with these, then see if there's anything more you can do.
Use hashes instead of the arrays that need to be searched using in_array(). This is so you can do a direct hash lookup in one step instead of converting the entire array to a hash for every iteration.
You don't need to check for symlinks because they will be skipped since you have not set the follow option.
Maximise your use of _; avoid repeating IO operations. _ is a special filehandle where the file status information is cached whenever you call stat() or any file test. This means you can call stat _ or -f _ instead of stat $name or -f $name. (Calling -f _ is more than 1000x faster than -f $name on my machine because it uses the cache instead of doing another IO operation.)
Use the Benchmark module to test out different optimisation strategies to see if you actually gain anything. E.g.
use Benchmark;
stat 'myfile.txt';
timethese(100_000, {
a => sub {-f _},
b => sub {-f 'myfile.txt'},
});
A general principle of performance tuning is find out exactly where the slow parts are before you try to tune it (because the slow parts might not be where you expect them to be). My recommendation is to use Devel::NYTProf, which can generate an html profile report for you. From the synopsis, on how to use it (from the command line):
# profile code and write database to ./nytprof.out
perl -d:NYTProf some_perl.pl
# convert database into a set of html files, e.g., ./nytprof/index.html
# and open a web browser on the nytprof/index.html file
nytprofhtml --open
发布评论
评论(1)
尝试从这些开始,然后看看是否还有其他可以做的事情。
使用散列代替需要使用
in_array()
搜索的数组。这样您就可以一步执行直接哈希查找,而不是在每次迭代时将整个数组转换为哈希。您不需要检查符号链接,因为您尚未设置
follow
选项,因此它们将被跳过。最大限度地利用
_
;避免重复IO操作。_
是一个特殊的文件句柄,每当您调用 stat() 或任何文件测试时,都会在其中缓存文件状态信息。这意味着您可以调用stat _
或-f _
而不是stat $name
或-f $name
。 (在我的机器上调用-f _
比-f $name
快 1000 倍以上,因为它使用缓存而不是执行另一个 IO 操作。)使用
Benchmark
模块测试不同的优化策略来看看你是否真的有所收获。例如性能调优的一般原则是在尝试调优之前准确找出慢速部分的位置(因为慢速部分可能不在您期望的位置)。我的建议是使用
Devel::NYTProf
,它可以为您生成一份html配置文件报告。从概要来看,如何使用它(从命令行):Try starting with these, then see if there's anything more you can do.
Use hashes instead of the arrays that need to be searched using
in_array()
. This is so you can do a direct hash lookup in one step instead of converting the entire array to a hash for every iteration.You don't need to check for symlinks because they will be skipped since you have not set the
follow
option.Maximise your use of
_
; avoid repeating IO operations._
is a special filehandle where the file status information is cached whenever you call stat() or any file test. This means you can callstat _
or-f _
instead ofstat $name
or-f $name
. (Calling-f _
is more than 1000x faster than-f $name
on my machine because it uses the cache instead of doing another IO operation.)Use the
Benchmark
module to test out different optimisation strategies to see if you actually gain anything. E.g.A general principle of performance tuning is find out exactly where the slow parts are before you try to tune it (because the slow parts might not be where you expect them to be). My recommendation is to use
Devel::NYTProf
, which can generate an html profile report for you. From the synopsis, on how to use it (from the command line):