Удивительно, но array2d немного быстрее!
Код: Выделить всё
#include
#include
#include
#include
#include
template
class array2d {
size_t nc{0}, nr{0};
std::vector vec{};
public:
array2d(size_t rows, size_t cols)
: nc(cols), nr(rows), vec(rows * cols) {}
template
auto&& operator[](this Self&& self, size_t i, size_t j) {
auto&& a = std::forward(self);
return a.vec[i * a.nc + j];
}
size_t rows() const { return nr; }
size_t cols() const { return nc; }
};
using Clock = std::chrono::high_resolution_clock;
using Duration = std::chrono::duration;
void benchmark_write() {
constexpr size_t ROWS = 5000;
constexpr size_t COLS = 5000;
constexpr int ITERATIONS = 10;
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79825988/why-is-my-user-defined-contiguous-2d-array-is-not-dramatically-faster-than-std[/url]
Мобильная версия