Multidimensional Arrays and std::mdspan

Performance Benefits of mdspan

What are the performance benefits of using mdspan compared to nested vectors or arrays?

Abstract art representing computer programming

Using std::mdspan can provide several performance benefits compared to using nested vectors or arrays for representing multidimensional data. Here are a few key advantages:

Contiguous Memory Layout

std::mdspan is designed to work with contiguous memory layouts, where all elements are stored in a single contiguous block of memory.

This is in contrast to nested vectors or arrays, where each inner vector or array is allocated separately, resulting in non-contiguous memory. Contiguous memory layout has several benefits:

Better Cache Locality

When accessing elements sequentially, contiguous memory allows for better utilization of the CPU cache, leading to faster memory access.

Reduced Memory Fragmentation

Contiguous memory allocation reduces memory fragmentation, which can improve overall memory usage efficiency.

Avoiding Memory Allocations

When using nested vectors or arrays, each inner vector or array requires its own memory allocation. This can lead to numerous small allocations, which can be inefficient and add overhead.

With std::mdspan, you typically allocate a single contiguous block of memory upfront (e.g., using std::vector or std::array) and then create an mdspan view over that memory. This avoids the need for multiple allocations and deallocations.

Efficient Element Access

std::mdspan provides an efficient way to access elements using multiple indices.

The indices are mapped to the corresponding position in the underlying contiguous memory block using a layout mapping function.

This mapping is typically optimized and can be more efficient compared to the nested indexing required with nested vectors or arrays.

Compatibility with Existing Code

std::mdspan is designed to be compatible with existing code that operates on contiguous memory, such as C-style arrays or single-dimensional std::vector or std::array.

This means you can easily integrate std::mdspan into existing codebases without major changes, while still benefiting from the performance advantages it offers.

Here's a simple example comparing element access using std::mdspan and nested vectors:

#include <chrono>
#include <iostream>
#include <mdspan>
#include <vector>

int main() {
  using namespace std::chrono;
  const int rows = 1000;
  const int cols = 1000;

  // Nested vectors
  std::vector<std::vector<int>> nestedVectors(
    rows, std::vector<int>(cols));

  // Contiguous memory with mdspan
  std::vector<int> contiguousData(rows * cols);
  std::mdspan<int, std::extents<
    std::size_t, rows, cols>> mdspanView{};

  // Measure access time for nested vectors
  auto startNested = high_resolution_clock::now();
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      nestedVectors[i][j] = i * cols + j;
  auto endNested = high_resolution_clock::now();

  // Measure access time for mdspan
  auto startMdspan = high_resolution_clock::now();
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      mdspanView[i, j] = i * cols + j;  
  auto endMdspan = high_resolution_clock::now();

  // Print access times
  duration<double> nestedTime =
    endNested - startNested;
  duration<double> mdspanTime =
    endMdspan - startMdspan;

  std::cout << "Nested vectors access time: "
    << nestedTime.count() << " seconds\n";

  std::cout << "mdspan access time: "
    << mdspanTime.count() << " seconds\n";

In this example, we compare the element access time for nested vectors and std::mdspan. The mdspan version typically demonstrates faster access times due to the benefits mentioned above.

Please note that the actual performance gains may vary depending on factors such as data size, access patterns, and hardware characteristics. It's always recommended to profile and benchmark your specific use case to determine the actual performance impact.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved