Умножение собственных матриц гораздо менее эффективно, чем обход цикла.C++

Программы на C++. Форум разработчиков
Anonymous
Умножение собственных матриц гораздо менее эффективно, чем обход цикла.

Сообщение Anonymous »

Код: Выделить всё

#include 
#include 
#include 
#include 

int main()
{
const int num_points = 300000000;

Eigen::Matrix init_points(num_points, 3);
Eigen::Matrix transformed_points(num_points, 3);

std::mt19937 rng(42);
std::uniform_real_distribution dist(-100.0f, 100.0f);

for (int i = 0; i < num_points; ++i)
{
init_points(i, 0) = dist(rng); // x
init_points(i, 1) = dist(rng); // y
init_points(i, 2) = dist(rng); // z
}

float theta = 3.14159265358 / 4; // pi/4
Eigen::Matrix3f rotation;
rotation = Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ());

Eigen::Vector3f translation(10.0f, 20.0f, 30.0f);

auto start_time = std::chrono::high_resolution_clock::now();

//transformed_points = init_points * rotation;   //uncomment this line to use the Matrix multiply version
//transformed_points.rowwise() += translation.transpose(); //uncomment this line to use the Matrix multiply version

for (int i = 0; i < num_points; ++i) //comment this for loop to use the Matrix multiply version
{
Eigen::Vector3f v = init_points.row(i).transpose();
v = rotation * v;
v += translation;
transformed_points.row(i) = v.transpose();
}

auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration duration_ms = end_time - start_time;
std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/79262191/eigen-matrix-multiplication-is-much-less-efficient-than-for-loop-traversal[/url]

Вернуться в «C++»