从另一个函数调用函数会增加执行时间吗? [英] does calling function from another function increase execution time?

查看:330
本文介绍了从另一个函数调用函数会增加执行时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



据说我们应该创建执行特定操作的函数。

我的问题是这样做了增加执行时间?



谢谢,

Dhaval Shah

Hello,

It is said that we should create functions that execute a specific action.
My question is that does doing so increase execution time?

Thanks,
Dhaval Shah

推荐答案

In一般是的,每次调用另一个方法时,CPU寄存器需要存储和恢复,并且必须将参数压入堆栈。

但是,根据各种因素,编译器可以优化它,主要是由模块的调用图驱动。

唯一可以确定的方法是编译代码,然后检查生成的IL代码(例如使用ILDASM)。



但是从代码可维护性角度来看更重要的是我不会从这些考虑开始,除非探查器指出这是一个实际问题。

代码结构,重用和可维护性应该推动您使用方法和分离的决策首先是有意义的块中的功能。
In general yes, every time you call another method the CPU registers need to be stored and restored and parameters have to be pushed on the stack.
However, depending on various factors the compiler can optimize this away, mainly driven by the call graph of your module.
The only way to know for sure is compiling the code and then inspecting the generated IL code (e.g. using ILDASM).

But more importantly from a code maintainability perspective I would not begin with these considerations, unless a profiler is indicating that this is an actual problem.
Code structure, reuse and maintainability should drive your decisions for using methods and separating functionalities in meaningful chunks in the first place.


这篇关于从另一个函数调用函数会增加执行时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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