什么是之间和QUOT的区别静态的QUOT;和"静态内联"功能? [英] What's the difference between "static" and "static inline" function?

查看:135
本文介绍了什么是之间和QUOT的区别静态的QUOT;和"静态内联"功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IMO既能使功能有翻译单位的范围而已,

什么是静态和静态内联的功能区别?

更新

为什么要在线放在头文件,而不是 .C 文件?


解决方案

在线指示编译器为尝试嵌入功能的内容进入调用code,而不是执行的实际通话。

对于那些经常叫,可以使一个很大的性能差异小的功能。

然而,这仅仅是一个暗示,编译器可以忽略它,而且大多数编译器将尝试内联,即使在不使用的关键词,作为优化,其中其可能的一部分。

例如:

 静态INT公司(int i)以{返回I + 1};
.... //一些code
INT I;
.... //一些code
对于(i = 0; I< 999999; I =公司(I)){/ *做的东西在这里* /};

这紧密的循环将执行在每个迭代函数调用,函数的内容实际上是显著低于code编译器需要把执行呼叫。 在线将主要指示编译器的code转换成以上的等效:

  INT I;
 ....
 对于(i = 0; I< 999999; I = I + 1){/ *做的东西在这里* /};

跳过实际的函数调用和返回

显然,这是为了显示这一点,不是真正的一块code的一个例子。

静态指范围。在C它意味着函数/变量只能在同一翻译单元内使用。

IMO both make the function to have a scope of translate unit only,

what's the difference between "static" and "static inline" function?

UPDATE

why should inline be put in header file, not .c file?

解决方案

inline instructs the compiler to attempt to embed the function content into the calling code instead of executing an actual call.

For small functions that are called frequently that can make a big performance difference.

However, this is only a "hint", and the compiler may ignore it, and most compilers will try to "inline" even when the keyword is not used, as part of the optimizations, where its possible.

for example:

static int Inc(int i) {return i+1};
.... // some code
int i;
.... // some more code
for (i=0; i<999999; i = Inc(i)) {/*do something here*/};

This tight loop will perform a function call on each iteration, and the function content is actually significantly less than the code the compiler needs to put to perform the call. inline will essentially instruct the compiler to convert the code above into an equivalent of:

 int i;
 ....
 for (i=0; i<999999; i = i+1) { /* do something here */};

Skipping the actual function call and return

Obviously this is an example to show the point, not a real piece of code.

static refers to the scope. In C it means that the function/variable can only be used within the same translation unit.

这篇关于什么是之间和QUOT的区别静态的QUOT;和&QUOT;静态内联&QUOT;功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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