内联函数在标头或实现中执行得更快? C ++ [英] Inline functions executed faster in header or implementation? C++

查看:142
本文介绍了内联函数在标头或实现中执行得更快? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:)
我们即将进行C ++考试,但我不知道这个可能的问题的答案:

在头文件或实现文件中定义内联函数时,执行内联函数的速度更快吗?

有人可以告诉我正确答案和原因吗?我真的很难理解这一点.提前非常感谢您! :)

我尝试过的事情:

我经历了我们的讲座并尝试对其进行研究,但是我发现的东西对我来说太复杂了:/

Hi :)
Our C++ exam is coming up and I don´t know the answer for this possible question:

Are inline functions executed faster when they are defined in the header file or the implementation file?

Could someone tell me the correct answer and the reason? I really struggle understanding this. Thank you so much in advance! :)

What I have tried:

I went through our lectures and tried to research it but the things that I did find are just too complicated for me :/

推荐答案

文件对于C ++编译不是必不可少的.本质上,文件级模块化只是项目开发,可维护性之类的问题,这很重要,但与编译结果无关.特别是.h和.cpp文件之间没有区别(我不是在谈论细微的细节,例如预编译的标头;我只是在谈论编译的结果.)一定顺序就可以了.

文件级模块化的概念基于C ++功能,允许多个声明和仅一个定义,然后基于对图像名称(字符串)进行操作的链接,仅此而已. (而且,坦率地说,这个概念简直是过时的;我一点都不喜欢;它与某些其他语言中使用的更高级的概念不匹配.预编译标头之类的技术不过是拐杖而已.) />
确实,内联函数可以使您获得一些次要的性能提升.请注意,编译器可能会或可能不会利用内联函数.您可以考虑将内联语法作为对编译器的建议.例如,在此处解释了所有内容:什么是C ++内联函数— C ++文章 [分析.

—SA
Files are not essential to C++ compilation. Essentially, file-level modularity is just a matter of project development, maintainability, things like that — very important but not related to the result of compilation. In particular, there is no difference between .h and .cpp files (I am not talking about subtle detail, such as precompiled headers; I''m talking only about the results of compilation.) The files are just fed to the compiler in certain order, that''s it.

The concept of file-level modularity is based on the C++ feature to allow multiple declarations and only one definition, and then on the linked which operates on the image names (string), that''s all. (And, frankly, this conception is awfully archaic; I don''t like it at all; it''s no match to more advanced concepts used in some other languages. The techniques like precompiled headers is nothing but crutches.)

Indeed, inline function can give you some minor gain in performance. Note that a compiler may or may not take the benefit of inline functions. You can consider inline syntax as a recommendation to the compiler. Everything is explained, for example, here: What is C++ inline functions — C++ Articles[^].

To find out how much is the influence of this technique in each particular environment, you can perform timing of operation or profiling.

—SA


也查看以下页面:标准C ++:内联函数 [ ^ ].
Have also a look at this page: Standard C++: Inline Functions[^].


报价:

当在头文件或实现文件中定义内联函数时,它们执行得更快吗? ?

Are inline functions executed faster when they are defined in the header file or the implementation file?

不快,不慢,速度完全相同,结果完全相同.
效率不取决于用于定义内联函数的文件扩展名.
即使您发明了自己的文件扩展名,也不会改变任何内容.

Not faster, not slower, exact same speed, exact same result.
Efficiency do not depend on the file extension used to define the inline function.
Even id you invent you own file extensions, it wouldn''t change anything.


这篇关于内联函数在标头或实现中执行得更快? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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