为什么在调用函数时会有开销? [英] Why is there overhead when calling functions?

查看:172
本文介绍了为什么在调用函数时会有开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,人们会说在程序中调用产生一定数量开销或不可避免的其他问题和情况的函数.与没有该函数调用的类似程序相比,可以更好地解释这一点吗?

Often, people speak of the calling of functions producing a certain amount of overhead, or an inescapable set of additional concerns and circumstances, in a program. Can this be better explained and compared to a similar program without the function call?

推荐答案

这取决于您的编译器设置及其优化代码的方式.内联一些功能.其他不是.通常取决于您是针对大小还是针对速度进行优化.

It depends on your compiler settings and the way it optimizes code. Some functions are inlined. Others are not. It usually depends on whether you're optimizing for size or for speed.

通常,调用函数会导致延迟,原因有两个:

Generally, calling function causes delay for two reasons:

  • 程序需要挂钩到函数代码开始的内存中的某个随机位置.为此,它需要将当前光标位置保存到堆栈中,以便知道返回的位置.此过程消耗多个CPU周期.

  • The program needs to hook to some random location in memory where your function code starts. To do this, it needs to save the current cursor position into a stack so it knows where to return. This process consumes more than one CPU cycle.

取决于您的CPU架构,可能会有一条管道,该管道与当前的指令执行并行地将下几条指令从内存中提取到CPU缓存中.这是为了加快执行速度.当您调用一个函数时,游标会挂接到一个完全不同的地址,并且所有缓存的指令都将从管道中清除.这会导致进一步的延误.

Depending on your CPU architecture, there may be a pipeline, which fetches the next few instruction from memory into the CPU cache in parallel with your current instruction execution. This is to speed up execution speed. When you call a function, the cursor hooks to a completely different address and all the cached instructions are flushed from the pipeline. This causes further delays.

这篇关于为什么在调用函数时会有开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆