有没有人见过关于在gcc / g ++中使用C / C ++ restrict
关键字是否在现实中提供任何显着的性能提升(而不仅仅是理论上)的任何数字/分析?
我已经阅读了各种推荐/贬低其使用的文章,但我没有碰到任何真实的数字,实际上证明了双方的论点。
修改
我知道restrict
并不是C ++的正式组成部分,但有些编译器支持它,我已经阅读了Christer Ericson的论文,强烈建议使用它。
答案 0 :(得分:46)
restrict关键字有所不同。
我在某些情况下(图像处理)看到了因子2和更多的改进。大多数时候,差异并不大。大约10%。
这是一个说明差异的小例子。我写了一个非常基本的4x4矢量*矩阵变换作为测试。请注意,我必须强制不要内联函数。否则,GCC会检测到我的基准测试代码中没有任何别名指针,并且由于内联,限制不会产生差异。
我本可以将转换功能移动到另一个文件中。
#include <math.h>
#ifdef USE_RESTRICT
#else
#define __restrict
#endif
void transform (float * __restrict dest, float * __restrict src,
float * __restrict matrix, int n) __attribute__ ((noinline));
void transform (float * __restrict dest, float * __restrict src,
float * __restrict matrix, int n)
{
int i;
// simple transform loop.
// written with aliasing in mind. dest, src and matrix
// are potentially aliasing, so the compiler is forced to reload
// the values of matrix and src for each iteration.
for (i=0; i<n; i++)
{
dest[0] = src[0] * matrix[0] + src[1] * matrix[1] +
src[2] * matrix[2] + src[3] * matrix[3];
dest[1] = src[0] * matrix[4] + src[1] * matrix[5] +
src[2] * matrix[6] + src[3] * matrix[7];
dest[2] = src[0] * matrix[8] + src[1] * matrix[9] +
src[2] * matrix[10] + src[3] * matrix[11];
dest[3] = src[0] * matrix[12] + src[1] * matrix[13] +
src[2] * matrix[14] + src[3] * matrix[15];
src += 4;
dest += 4;
}
}
float srcdata[4*10000];
float dstdata[4*10000];
int main (int argc, char**args)
{
int i,j;
float matrix[16];
// init all source-data, so we don't get NANs
for (i=0; i<16; i++) matrix[i] = 1;
for (i=0; i<4*10000; i++) srcdata[i] = i;
// do a bunch of tests for benchmarking.
for (j=0; j<10000; j++)
transform (dstdata, srcdata, matrix, 10000);
}
结果:(在我的2 Ghz Core Duo上)
nils@doofnase:~$ gcc -O3 test.c
nils@doofnase:~$ time ./a.out
real 0m2.517s
user 0m2.516s
sys 0m0.004s
nils@doofnase:~$ gcc -O3 -DUSE_RESTRICT test.c
nils@doofnase:~$ time ./a.out
real 0m2.034s
user 0m2.028s
sys 0m0.000s
在那个系统上,执行速度提高了20%。
为了显示它依赖于架构的程度,我让相同的代码在Cortex-A8嵌入式CPU上运行(稍微调整了循环次数,因为我不想等那么久):
root@beagleboard:~# gcc -O3 -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp test.c
root@beagleboard:~# time ./a.out
real 0m 7.64s
user 0m 7.62s
sys 0m 0.00s
root@beagleboard:~# gcc -O3 -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -DUSE_RESTRICT test.c
root@beagleboard:~# time ./a.out
real 0m 7.00s
user 0m 6.98s
sys 0m 0.00s
这里差异只有9%(编译器顺便说一句。)
答案 1 :(得分:8)
restrict关键字是否在gcc / g ++中提供了显着的好处?
可以减少指令数量,如下例所示,因此请尽可能使用它。
GCC 4.8 Linux x86-64 exmample
输入:
void f(int *a, int *b, int *x) {
*a += *x;
*b += *x;
}
void fr(int *restrict a, int *restrict b, int *restrict x) {
*a += *x;
*b += *x;
}
编译和反编译:
gcc -g -std=c99 -O0 -c main.c
objdump -S main.o
使用-O0
,它们是相同的。
使用-O3
:
void f(int *a, int *b, int *x) {
*a += *x;
0: 8b 02 mov (%rdx),%eax
2: 01 07 add %eax,(%rdi)
*b += *x;
4: 8b 02 mov (%rdx),%eax
6: 01 06 add %eax,(%rsi)
void fr(int *restrict a, int *restrict b, int *restrict x) {
*a += *x;
10: 8b 02 mov (%rdx),%eax
12: 01 07 add %eax,(%rdi)
*b += *x;
14: 01 06 add %eax,(%rsi)
对于没有经验的人,calling convention是:
rdi
=第一个参数rsi
=第二个参数rdx
=第三个参数结论: 3条指令代替4条。
当然,说明can have different latencies,但这提供了一个好主意。
为什么GCC能够对其进行优化?
上面的代码取自Wikipedia example,非常有启发性。
f
的伪装配:
load R1 ← *x ; Load the value of x pointer
load R2 ← *a ; Load the value of a pointer
add R2 += R1 ; Perform Addition
set R2 → *a ; Update the value of a pointer
; Similarly for b, note that x is loaded twice,
; because a may be equal to x.
load R1 ← *x
load R2 ← *b
add R2 += R1
set R2 → *b
fr
:
load R1 ← *x
load R2 ← *a
add R2 += R1
set R2 → *a
; Note that x is not reloaded,
; because the compiler knows it is unchanged
; load R1 ← *x
load R2 ← *b
add R2 += R1
set R2 → *b
它真的更快吗?
嗯...不是这个简单的测试:
.text
.global _start
_start:
mov $0x10000000, %rbx
mov $x, %rdx
mov $x, %rdi
mov $x, %rsi
loop:
# START of interesting block
mov (%rdx),%eax
add %eax,(%rdi)
mov (%rdx),%eax # Comment out this line.
add %eax,(%rsi)
# END ------------------------
dec %rbx
cmp $0, %rbx
jnz loop
mov $60, %rax
mov $0, %rdi
syscall
.data
x:
.int 0
然后:
as -o a.o a.S && ld a.o && time ./a.out
在Ubuntu 14.04 AMD64 CPU Intel i5-3210M上。
我承认我仍然不了解现代CPU。如果您:让我知道:
答案 2 :(得分:6)
文章Demystifying The Restrict Keyword引用了论文Why Programmer-specified Aliasing is a Bad Idea(pdf),该文件说它通常没有用,并提供测量来支持这一点。
答案 3 :(得分:0)
请注意,允许restrict
关键字的C ++编译器仍然可以忽略它。例如here就是这种情况。
答案 4 :(得分:-1)
我测试了this C-Program。如果没有restrict
,则需要12.640秒才能完成restrict
12.516。看起来可以保存一些时间。