C 中的内联函数 v. 宏——开销是多少(内存/速度)? [英] Inline function v. Macro in C -- What's the Overhead (Memory/Speed)?

查看:23
本文介绍了C 中的内联函数 v. 宏——开销是多少(内存/速度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Stack Overflow 中搜索了类似函数的宏与内联函数的优缺点.

I searched Stack Overflow for the pros/cons of function-like macros v. inline functions.

我发现了以下讨论:不同宏函数/内联方法的优缺点在 C 中

...但它没有回答我最迫切的问题.

...but it didn't answer my primary burning question.

也就是说,在内存使用和执行速度方面,使用宏函数(带有变量,可能还有其他函数调用)v. 内联函数在 c 中的开销是多少?

Namely, what is the overhead in c of using a macro function (with variables, possibly other function calls) v. an inline function, in terms of memory usage and execution speed?

在开销方面是否存在任何依赖于编译器的差异?我可以使用 icc 和 gcc.

Are there any compiler-dependent differences in overhead? I have both icc and gcc at my disposal.

我正在模块化的代码片段是:

My code snippet I'm modularizing is:

double AttractiveTerm = pow(SigmaSquared/RadialDistanceSquared,3);
double RepulsiveTerm = AttractiveTerm * AttractiveTerm;
EnergyContribution += 
   4 * Epsilon * (RepulsiveTerm - AttractiveTerm);

我把它变成内联函数/宏的原因是我可以把它放到一个 c 文件中,然后有条件地编译其他类似但略有不同的函数/宏.

My reason for turning it into an inline function/macro is so I can drop it into a c file and then conditionally compile other similar, but slightly different functions/macros.

例如:

double AttractiveTerm = pow(SigmaSquared/RadialDistanceSquared,3);
double RepulsiveTerm = pow(SigmaSquared/RadialDistanceSquared,9);
EnergyContribution += 
   4 * Epsilon * (RepulsiveTerm - AttractiveTerm);

(注意第二行的区别……)

(note the difference in the second line...)

这个函数是我的代码的核心函数,在我的程序中每一步都会被调用数千次,我的程序会执行数百万步.因此,我希望尽可能减少开销,因此我为什么要浪费时间担心内联和将代码转换为宏的开销.

This function is a central one to my code and gets called thousands of times per step in my program and my program performs millions of steps. Thus I want to have the LEAST overhead possible, hence why I'm wasting time worrying about the overhead of inlining v. transforming the code into a macro.

根据之前的讨论,我已经意识到宏的其他优点/缺点(类型独立性和由此产生的错误)......但我最想知道但目前不知道的是性能.

Based on the prior discussion I already realize other pros/cons (type independence and resulting errors from that) of macros... but what I want to know most, and don't currently know is the PERFORMANCE.

我知道你们中的一些 C 退伍军人会对我有一些深刻的见解!

I know some of you C veterans will have some great insight for me!!

推荐答案

调用内联函数可能会也可能不会生成函数调用,这通常会产生非常少量的开销.inline 函数实际被内联的确切情况因编译器而异.大多数人真诚地努力内联小函数(至少在启用优化时),但没有要求他们这样做(C99,§6.7.4):

Calling an inline function may or may not generate a function call, which typically incurs a very small amount of overhead. The exact situations under which an inline function actually gets inlined vary depending on the compiler; most make a good-faith effort to inline small functions (at least when optimization is enabled), but there is no requirement that they do so (C99, §6.7.4):

使函数成为内联函数建议对函数的调用是尽可能快地.程度为哪些这样的建议是有效的是实现定义的.

Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

宏不太可能产生这样的开销(尽管同样,几乎没有什么可以阻止编译器以某种方式做某事;标准没有定义机器代码程序必须扩展到什么,只定义了编译程序的可观察行为).

A macro is less likely to incur such overhead (though again, there is little to prevent a compiler from somehow doing something; the standard doesn't define what machine code programs must expand to, only the observable behavior of a compiled program).

使用任何更清洁的东西.轮廓.如果重要,请做一些不同的事情.

Use whatever is cleaner. Profile. If it matters, do something different.

还有,fizzer 说了什么;对 pow (和除法)的调用通常都比函数调用开销更昂贵.最小化这些是一个好的开始:

Also, what fizzer said; calls to pow (and division) are both typically more expensive than function-call overhead. Minimizing those is a good start:

double ratio = SigmaSquared/RadialDistanceSquared;
double AttractiveTerm = ratio*ratio*ratio;
EnergyContribution += 4 * Epsilon * AttractiveTerm * (AttractiveTerm - 1.0);

EnergyContribution 是否仅由看起来像这样的术语组成?如果是这样,把 4 * Epsilon 拉出来,每次迭代保存两个乘法:

Is EnergyContribution made up only of terms that look like this? If so, pull the 4 * Epsilon out, and save two multiplies per iteration:

double ratio = SigmaSquared/RadialDistanceSquared;
double AttractiveTerm = ratio*ratio*ratio;
EnergyContribution += AttractiveTerm * (AttractiveTerm - 1.0);
// later, once you've done all of those terms...
EnergyContribution *= 4 * Epsilon;

这篇关于C 中的内联函数 v. 宏——开销是多少(内存/速度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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