有谁知道如何 boost boost::numeric::ublas::vector
?
我正在使用typedef ublas::vector<float, ublas::bounded_array<float, 3> > MYVECTOR3
并将其速度与 D3DXVECTOR3
进行比较在简单的操作上。
测试如下所示:
#include <d3dx9.h>
#pragma comment(lib, "d3dx9.lib")
static const size_t kRuns = static_cast<size_t>(10e6);
TEST(Performance, CStyleVectors) {
D3DXVECTOR3 a(1.0f, 2.0f, 3.0f);
D3DXVECTOR3 b(2.0f, 3.0f, 1.0f);
D3DXVECTOR3 c(6.0f, 4.0f, 5.0f);
for (size_t i = 0; i < kRuns; ++i) {
c = c + (a + b) * 0.5f;
}
}
#include <boost/numeric/ublas/vector.hpp>
TEST(Performance, CppStyleVectors) {
typedef boost::numeric::ublas::vector<float,
boost::numeric::ublas::bounded_array<float, 3> > MYVECTOR3;
MYVECTOR3 a(3), b(3), c(3);
a[0] = 1.0f, a[1] = 2.0f, a[2] = 3.0f;
b[0] = 2.0f, b[1] = 3.0f, b[2] = 1.0f;
c[0] = 6.0f, c[1] = 4.0f, c[2] = 5.0f;
for (size_t i = 0; i < kRuns; ++i) {
noalias(c) = c + (a + b) * 0.5f;
}
}
结果如下:
[----------] 2 tests from Performance
[ RUN ] Performance.CStyleVectors
[ OK ] Performance.CStyleVectors (484 ms)
[ RUN ] Performance.CppStyleVectors
[ OK ] Performance.CppStyleVectors (9406 ms)
[----------] 2 tests from Performance (9890 ms total)
正如您所看到的,纯 C 风格 vector 比 boost::numeric::ublas
中的 vector 快大约 20 倍。即使使用自定义的基于堆栈的分配器也是如此。有人知道我如何加快速度吗?
也许可以通过编写自定义包装器或类似的东西?
谢谢
最佳答案
Boost uBLAS(以及一般的 BLAS)提供对 vector 和矩阵代数的支持,其中维数在运行时确定。它适用于解决某些数值问题(例如使用有限元或类似方法进行模拟、优化问题、近似)。对于这些问题,它的速度相对较快,但在性能方面无法与其地盘上专门的 3d vector 类库竞争。
使用其他一些库。如果 D3DXVECTOR3 还不够,请查看例如CGAL。
关于C++ vector 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3258789/