我有一个 Perl 函数,它不返回任何值。它也不需要任何参数。
sub test {
#do my logic
}
我可以这样做:
sub test() {
#do my logic
}
请问子程序测试 内联?这会起作用吗? (意味着函数调用将被函数定义替换。我的程序会执行得更快吗?)
函数 test() 被调用了 5000 次。而且我的 Perl 程序需要比预期更长的时间来执行。所以我想让我的程序更快。
提前致谢!
最佳答案
这是在 Constant Functions in perlsub 中回答的
Functions with a prototype of
()
are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without&
. Calls made using&
are never inlined. (Seeconstant.pm
for an easy way to declare most constants.)
所以你的
sub test()
如果满足上述条件,则应内联。没有看到函数,没有人能知道,所以要么展示它,要么尝试。使用 B::Deparse 最容易检查。 ,请进一步查看链接
perlsub
部分。我会敦促您分析程序以确保函数调用开销是问题所在。
关于perl - 带内联的子程序 perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50921578/