【发布时间】:2014-07-19 01:35:54
【问题描述】:
我可以通过 CPU 分析器看到,compute_variances() 是我项目的瓶颈。
% cumulative self self total
time seconds seconds calls ms/call ms/call name
75.63 5.43 5.43 40 135.75 135.75 compute_variances(unsigned int, std::vector<Point, std::allocator<Point> > const&, float*, float*, unsigned int*)
19.08 6.80 1.37 readDivisionSpace(Division_Euclidean_space&, char*)
...
这是函数的主体:
void compute_variances(size_t t, const std::vector<Point>& points, float* avg,
float* var, size_t* split_dims) {
for (size_t d = 0; d < points[0].dim(); d++) {
avg[d] = 0.0;
var[d] = 0.0;
}
float delta, n;
for (size_t i = 0; i < points.size(); ++i) {
n = 1.0 + i;
for (size_t d = 0; d < points[0].dim(); ++d) {
delta = (points[i][d]) - avg[d];
avg[d] += delta / n;
var[d] += delta * ((points[i][d]) - avg[d]);
}
}
/* Find t dimensions with largest scaled variance. */
kthLargest(var, points[0].dim(), t, split_dims);
}
kthLargest() 似乎不是问题,因为我看到了:
0.00 7.18 0.00 40 0.00 0.00 kthLargest(float*, int, int, unsigned int*)
compute_variances() 采用浮点向量的向量(即Points 的向量,其中Points 是我实现的一个类)并计算它们在每个维度上的方差(关于算法克努斯的)。
这是我调用函数的方式:
float avg[(*points)[0].dim()];
float var[(*points)[0].dim()];
size_t split_dims[t];
compute_variances(t, *points, avg, var, split_dims);
问题是,我能做得更好吗?我真的很乐意在速度和方差的近似计算之间做出权衡。或者也许我可以让代码对缓存更友好?
我是这样编译的:
g++ main_noTime.cpp -std=c++0x -p -pg -O3 -o eg
请注意,在编辑之前,我使用了-o3,而不是大写的“o”。感谢ypnos,我现在使用优化标志-O3 进行编译。我确信它们之间存在差异,因为我在我的伪站点中使用 these methods 之一进行了时间测量。
请注意,现在,compute_variances 正在主导整个项目的时间!
[编辑]
copute_variances() 被调用了 40 次。
每 10 次调用,以下情况成立:
points.size() = 1000 and points[0].dim = 10000
points.size() = 10000 and points[0].dim = 100
points.size() = 10000 and points[0].dim = 10000
points.size() = 100000 and points[0].dim = 100
每个调用处理不同的数据。
问:访问points[i][d] 的速度有多快?
答:point[i] 只是 std::vector 的第 i 个元素,其中第二个 [] 在 Point 类中实现为这样。
const FT& operator [](const int i) const {
if (i < (int) coords.size() && i >= 0)
return coords.at(i);
else {
std::cout << "Error at Point::[]" << std::endl;
exit(1);
}
return coords[0]; // Clear -Wall warning
}
其中coords 是std::vector 的float 值。这似乎有点重,但是编译器不应该足够聪明以正确预测分支总是正确的吗? (我的意思是在冷启动之后)。此外,std::vector.at() 应该是恒定时间(如ref 中所述)。我将其更改为在函数主体中仅包含 .at(),并且时间测量值几乎保持不变。
compute_variances() 中的除法 肯定很重!但是,Knuth 的算法是数值稳定的算法,我无法找到另一种既数值稳定又无需除法的算法。
请注意,我现在不对并行感兴趣。
[EDIT.2]
Point 类的最小示例(我想我没有忘记展示一些东西):
class Point {
public:
typedef float FT;
...
/**
* Get dimension of point.
*/
size_t dim() const {
return coords.size();
}
/**
* Operator that returns the coordinate at the given index.
* @param i - index of the coordinate
* @return the coordinate at index i
*/
FT& operator [](const int i) {
return coords.at(i);
//it's the same if I have the commented code below
/*if (i < (int) coords.size() && i >= 0)
return coords.at(i);
else {
std::cout << "Error at Point::[]" << std::endl;
exit(1);
}
return coords[0]; // Clear -Wall warning*/
}
/**
* Operator that returns the coordinate at the given index. (constant)
* @param i - index of the coordinate
* @return the coordinate at index i
*/
const FT& operator [](const int i) const {
return coords.at(i);
/*if (i < (int) coords.size() && i >= 0)
return coords.at(i);
else {
std::cout << "Error at Point::[]" << std::endl;
exit(1);
}
return coords[0]; // Clear -Wall warning*/
}
private:
std::vector<FT> coords;
};
【问题讨论】:
-
什么是值 - points.size 和 points[0].dim - 运行 32 秒?访问点数[i][d]的速度有多快?
-
几个超级快速的观察结果:如果每个
compute_variances()批次的点的维数是一致的,那么在该值上模板化函数可能会让编译器更有效地展开内部循环。此外,正如其他人所说,每次迭代的划分可能会主导您的时间成本。由于存在多个维度,您不能只对数组进行一次数值稳定性预排序,但如果这是您的瓶颈,您可能需要考虑其他方差算法。 -
@MBo 已更新。 Jeff 我不确定你所说的统一维度是什么意思,但我认为更新也会涵盖你。
-
我们确实需要查看
Point类的整个 声明。例如,如果dim()不是 const 方法,则内部循环将非常低效。 -
@KubaOber,我在考虑是否应该发布
Point课程。dim()是常量。我将发布最小的Point类。
标签: c++ algorithm optimization variance