C语言讲义——内联函数

  • 如果一些函数被频繁调用,不断地有函数入栈(Stack),会造成栈空间的大量消耗。
  • 对应这种问题,可以使用内联函数(inline)。
  • 编译器会将内联函数的代码整段插入到调用的位置。
#include <stdio.h>

inline int parity(int a) {
	return (a % 2 > 0) ? 1 : 0;
}

int main() {
	int i = 0;
	for (i=1; i < 10000; i++) {
		printf("i:%d    奇偶性:%d 
", i, parity(i));
	}
}

执行时间:
不用内联函数:5.572s
使用内联函数:2.847s

原文地址:https://www.cnblogs.com/tigerlion/p/11191704.html