perl - 带内联的子程序 perl

标签 perl optimization inline subroutine

我有一个 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. (See constant.pm for an easy way to declare most constants.)



所以你的 sub test()如果满足上述条件,则应内联。没有看到函数,没有人能知道,所以要么展示它,要么尝试。

使用 B::Deparse 最容易检查。 ,请进一步查看链接 perlsub部分。

我会敦促您分析程序以确保函数调用开销是问题所在。

关于perl - 带内联的子程序 perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50921578/

相关文章:

javascript - 使用 AngularJS 读取和处理 html

linux - 如果在 perl 脚本中运行 cp 是否会覆盖存在的文件?

mysql - 优化和/或重新架构大型 MySQL 表的建议

perl - 如何在 Perl 中打开一组文件?

perl - 使用 PAR Packer 进行编码时需要包含哪些内容

css - float 用作内联样式,但当我将其移至外部样式表时则不起作用

c++ - 对于可变长度函数指针,在 C++ 中使用内联函数或 MACROS 哪个更好

html - 如何仅使用 CSS 强制 <div> 与复选框内联?

php - PHP错误处理: my code is not optimized

mysql - 优化大型 MySQL 表中的索引