假设缓冲区是一个由多个成员组成的结构,包括一个数组。
使用通常的优化标志进行编译,运行这两个代码片段应该会有什么样的性能差异?
buffer buf;
for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
init(huge_file, i, &buf);
}
// buf is not used afterward
...
void init(FILE* f, int i, buffer* b) {
... // b is filled using f, according to i
do_something(b);
}
对比
for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
init(huge_file, i);
}
...
void init(FILE* f, int i) {
buffer buf;
... // buf is filled from scratch using f, according to i
do_something(&buf);
}