简体   繁体   English

函数调用的数量对性能有多大影响?

[英]how much effect does number of function calls have on performance?

Does calling more functions have any kind of noticeable effect of performance, or is trying to cut down on number of function calls a pointless premature optimization? 调用更多函数是否会产生任何明显的性能影响,或者是否试图减少函数调用的数量而进行无意义的过早优化?

Simplified Example: Is there any real performance example between this: 简化示例:这之间是否有任何真实的性能示例:

function foo($var) {
    echo $var;
}
foo();

And This: 和这个:

function foo($var) {
   bar($var);
}

function bar($var) {
  baz($var);
}

function baz($var) {
  echo $var;
}

foo();

It's premature optimization. 这是不成熟的优化。 You should have functions that make sense based on your domain and the logical separation of tasks in your code. 根据您的域以及代码中任务的逻辑分离,您应该具有有意义的功能。 The only time I would ever consider replacing a function with inline code is if it was in a tight loop and a profiler showed it was a problem. 我唯一一次考虑用内联代码替换一个函数是因为它是一个紧凑的循环而且一个分析器显示它是一个问题。

To more directly answer your question, the effect on performance hinges on quite a few things: 为了更直接地回答你的问题,对性能的影响取决于很多事情:

  1. How often is the function called? 这个函数多久被调用一次?
  2. Is it in a critical section of the code? 它是否在代码的关键部分?
  3. How large is/are the function(s)? 功能有多大?

Deciding on whether something should be in a function should be driven by logical separation, clarity, maintainability, etc. and not performance. 决定某项功能是否属于功能应该由逻辑分离,清晰度,可维护性等驱动,而不是性能。

Always do profiling first, then bother with optimizations. 始终先进行性能分析,然后再进行优化。

On the other hand, function calls do have some overhead, but you should really profile your code before you decide to optimize out function calls. 另一方面,函数调用确实有一些开销,但在决定优化函数调用之前,您应该真正地编写代码。

To answer your question in the simplest way No. 以最简单的方式回答你的问题。

Now for the longer answer, function overhead is so small that you won't begin to notice it until you've made at least several million calls on a particular function. 现在,对于更长的答案,函数开销非常小,在您对特定函数进行至少几百万次调用之前,您不会开始注意它。 In most UI programs that very rarely happens having millions of calls to a method. 在大多数UI程序中,很少发生对方法的数百万次调用。 What's inside your function is going to contribute vastly more to your performance than the overhead associated with calling the function. 与调用函数相关的开销相比,函数内部对性能的贡献要大得多。

To further illustrate my point. 进一步说明我的观点。 Computers operate on the nanosecond timeframe. 计算机在纳秒时间范围内运行。 Humans can detect about 10 milliseconds (from UI testing). 人类可以检测到大约10毫秒(来自UI测试)。 1ms = 1.0 × 10^6 ns. 1ms = 1.0×10 ^ 6 ns。 So if it takes 1ns to call your function you'd need to call it between 1 million to 10 million times before a human could tell the difference. 因此,如果需要1ns来调用你的功能,你需要在100万到1000万次之间调用它,然后人类可以分辨出来。

If an function's contents is expensive to call then reducing when you call it can help performance, but again that has everything to do with what's inside the function and NOT the act of calling the function. 如果一个函数的内容调用起来很昂贵,那么在调用它时减少它可以帮助提高性能,但是这又与函数内部的内容有关,而不是调用函数的行为。

Never try and optimize before you've measured it. 在测量之前切勿尝试优化。 I'll say it again never never try and predict what will be performant in your application. 我会再次说它永远不会尝试预测你的应用程序中的性能。 Always measure performance with a profiler. 始终使用分析器测量性能。 It's like trying to predict the bugs in your program before you've written it. 这就像在编写程序之前尝试预测程序中的错误一样。

The overhead of a function call is pretty stiff actually. 函数调用的开销实际上非常僵硬。 But it's still low in the grand scheme of things. 但在宏伟的计划中它仍然很低。 If you're only calling the chain a handful of times in the execution, don't worry about it. 如果您在执行过程中只对链条进行了几次调用,请不要担心。 If you're going to be looping a lot , then you may want to consider optimizing it a little bit. 如果您要循环很多 ,那么您可能需要考虑优化它。

But remember, the biggest performance gain is the transition from a non-working state to a working one. 但请记住,最大的性能提升是从非工作状态转变为工作状态。 Only then start tackling the actual bottlenecks... And never forget: Premature optimization is the root of all evil ... 只有这样才能开始解决实际的瓶颈......永远不要忘记: Premature optimization is the root of all evil ......

I wouldn't worry about speed so much as about memory, since each function call costs stack. 我不会担心速度与内存有关,因为每个函数调用都需要堆栈。 Again, worrying about this in examples like yours would be premature optimization, but it's good to have it in mind if you're planning for a recursive algorithm in a language that's not optimized to handle recursive function calls. 再次,在像你这样的例子中担心这一点会是过早的优化,但如果你计划用一种没有优化处理递归函数调用的语言的递归算法,那么记住它是件好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM