简体   繁体   English

具有和不具有参数C / C ++的函数之间的执行时间

[英]Execution time between functions with & without arguments C/C++

I'd just like to ask what's the difference in execution time when: 我只想问以下情况下执行时间有何不同?

a) We have variables declared in main() and send them in functions using arguments a)我们在main()声明了变量,并使用参数在函数中将其发送

b) We have global variables and access them directly from functions with no arguments b)我们有全局变量,可以直接从不带参数的函数中访问它们

The thing is I started writing few aps and just saw a few discussions about this, but I haven't yet written any application that makes the difference bigger then few ms. 关键是我开始编写一些Aps,并且看到了一些有关此的讨论,但是我还没有编写任何使两者之间的差异大几毫秒的应用程序。

Passing parameters to function involves usually one of two assembler instructions: push param (and later pop param ) or mov ax, param . 将参数传递给函数通常涉及以下两个汇编程序指令之一: push param (和后来的pop param )或mov ax, param Since processor is able to do (a lot) more in a second, such "optimization" mostly probably will go unnoticed (a few ms on the whole program is below the error margin) 由于处理器能够在一秒钟内完成更多(很多)操作,因此这种“优化”很可能不会被注意到(整个程序的几毫秒低于错误余量)

Using global variables in the place of function parameters will cause a huge mess in the code with almost unnoticeable or possibly no performance gain. 使用全局变量代替函数参数会导致代码混乱,几乎不会引起注意,甚至可能不会提高性能。

All of this depends on what CPU and compiler that are used. 所有这些取决于所使用的CPU和编译器。

When you pass a parameter to a function, one of the following could happen: 当您将参数传递给函数时,可能会发生以下情况之一:

  • The parameter is stored in a CPU register. 该参数存储在CPU寄存器中。 This is very efficient. 这非常有效。
  • The parameter is stored on the stack. 该参数存储在堆栈中。 This is the most common. 这是最常见的。 It involves some minor overhead of pushing/popping the parameter to/from the stack when the function is started/finished. 函数启动/完成时,将参数压入/从堆栈中弹出/弹出堆栈会涉及一些较小的开销。
  • The parameter isn't a new variable at all. 该参数根本不是新变量。 Instead, the compiler inlines the function and uses the original variable for modifications. 而是,编译器内联函数并使用原始变量进行修改。 This is about as efficient as you can get. 这大约是您可以获得的最高效率。

Using a global variable is will be ever so slightly faster than using the stack. 使用全局变量将比使用堆栈快得多。 It will unlikley be faster than using a CPU register: inside the function the value might need to be loaded into such a register before calculations anyhow. 它绝对比使用CPU寄存器快:在函数内部,无论如何计算,可能需要将该值加载到此类寄存器中。

It should be noted we are talking about a few CPU ticks here and there. 应该注意的是,我们在这里和那里谈论的是几个CPU滴答声。

My advise: 我的建议:

  • You should never attempt any kind of manual optimizations unless you have in-depth hardware knowledge of the specific CPU used. 除非您对所使用的特定CPU有深入的硬件知识,否则切勿尝试任何形式的手动优化。 If you have no such knowledge, the compiler will optimize the code better than you do in 99% of the cases. 如果您没有这种知识,那么在99%的情况下,编译器将比您更好地优化代码。 Because the compiler port was most likely written by an expert of the given system. 因为编译器端口很可能是给定系统的专家编写的。 The compiler also knows the overall performance picture, which the programmer does not, so the compiler is more suitable for doing the optimization. 编译器还知道整体性能情况,而程序员则不知道,因此编译器更适合进行优化。
  • You should never attempt any kind of manual optimization unless you have actually performed formal benchmarking and found a bottleneck in the program. 除非您实际执行了正式的基准测试并在程序中发现了瓶颈,否则切勿尝试任何形式的手动优化。
  • Global variables are incredibly bad and dangerous to use. 全局变量使用起来非常糟糕且危险。 They lead to spaghetti code and they are not thread-safe. 它们导致意大利面条代码,并且不是线程安全的。
  • If you are writing some kind of high-end desktop application, such as PC programs or phone apps, then using global variables to increase performance is complete nonsense . 如果您正在编写某种高端桌面应用程序,例如PC程序或电话应用程序,那么使用全局变量来提高性能完全是胡说八道 You are on a system which has non-existent real-time performance to begin with! 首先,您所处的系统没有实时性能! At any given time, your OS might decide to chew up many billions of CPU ticks while giving your program the finger. 在任何给定的时间,您的OS可能会决定在给程序指指点点的情况下咀嚼数十亿个CPU滴答声。 So don't go chasing 1 or 2 CPU ticks. 因此,不要追逐1或2个CPU滴答声。
  • These kind of manual optimizations only make sense if you are developing embedded systems applications that are very close to the hardware, and at the same time have hard real-time requirements. 仅当您正在开发与硬件非常接近且同时具有严格实时要求的嵌入式系统应用程序时,此类手动优化才有意义。

Stack operations are very efficient, and they are made even more efficient because the stack is likely to be in cache memory. 堆栈操作非常高效,由于堆栈可能位于高速缓存中,因此它们的工作效率更高。 That could make using the stack even faster than not using it. 这可能会使使用堆栈比不使用堆栈更快。

Expect parameter passed variables to be a lot faster than global variables. 期望参数传递的变量比全局变量快很多。 In Modern ABIs, function parameters are passed mostly using CPU registers which are immediately available to the CPU. 在现代ABI中,大多数使用CPU寄存器传递功能参数,而CPU寄存器可立即使用。

Global variables must be read from (static) memory. 全局变量必须从(静态)内存中读取。 Worse than that, static memory is allocated on its own memory page, which is generally far from your stack (or heap) memory. 更糟糕的是,静态内存是在其自己的内存页面上分配的,该页面通常离您的堆栈(或堆)内存很远。 This means that cache misses are more likely which in turn means that access can consume a large number of CPU cycles. 这意味着高速缓存未命中的可能性更高,这又意味着访问可能消耗大量CPU周期。

Obviously this depends a lot on your usage patterns. 显然,这很大程度上取决于您的使用模式。

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

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