我写了一个简单的代码来测试prof
。
double bar_compute (double d) {
double t = std::abs(d);
t += std::sqrt(d);
t += std::cos(d);
return t;
}
// Do some computation n times
double foo_compute(unsigned n) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(0.0, 1.0);
double total = 0;
for (int i=0; i<n; i++) {
double d = dist(mt);
total += bar_compute(d);
}
return total;
}
当我运行prof
并查看输出时,它是
56.14% runcode libm-2.23.so [.] __cos_avx
27.34% runcode runcode [.] _Z11foo_computej
13.92% runcode runcode [.] _Z11bar_computed
0.86% runcode libm-2.23.so [.] do_cos_slow.isra.1
0.44% runcode runcode [.] cos@plt
0.41% runcode libm-2.23.so [.] sloww1
0.35% runcode libm-2.23.so [.] __dubcos
0.17% runcode ld-2.23.so [.] _dl_lookup_symbol_x
do_cos_slow.isra
和sloww1
是什么意思?
有没有更快的cos
版本可以使用?否则,为什么它会被称为慢?
发布于 2019-03-18 14:14:19
do_cos_slow
来自于它在glibc/sysdeps/ieee754/dbl-64/s_sin.c中的声明。它之所以被称为do_cos_slow
,是因为它比它基于do_cos
的函数更精确,因为它在Line 164上的声明上面的注释。
.isra
是因为该函数是已由IPA SRA根据以下Stack Overflow Answer, What does the GCC function suffix “isra” mean?优化的版本
sloww1
是一个根据上面的注释计算sin(x+dx)的函数。
关于更快的cos版本,我不确定是否有更快的版本,但如果你更新你的glibc或libc实现,提供libm,至少glibc 2.28,那么你会得到Wilco Dijkstra删除这些慢路径函数和重构dosincos的结果,这会提高速度。
Refactor the sincos implementation - rather than rely on odd partial inlining
of preprocessed portions from sin and cos, explicitly write out the cases.
This makes sincos much easier to maintain and provides an additional 16-20%
speedup between 0 and 2^27. The overall speedup of sincos is 48% over this range.
Between 0 and PI it is 66% faster.
您可以尝试的其他替代方案是其他libc或libm实现,或者包括avx_mathfun、avx_mathfun with some fixes for newer GCC或supersimd在内的其他cos实现。
https://stackoverflow.com/questions/55214621
复制相似问题