Fast多维数组

#include<iostream>
#include<chrono>

struct Timer
{
	std::chrono::time_point<std::chrono::steady_clock>start, end;
	std::chrono::duration<float>duration;

	Timer()
	{
		start = std::chrono::high_resolution_clock::now();
	}

	~Timer()
	{
		end = std::chrono::high_resolution_clock::now();
		duration = end - start;

		float ms = duration.count() * 1000.0f;
		std::cout << "Timer took " << ms << " ms" << std::endl;
	}
};
struct Rgb
{
	int r;
	int g;
	int b;
};

#define M 8000
#define N 5000

void draw1()
{
	Timer timer;
	Rgb* a = new Rgb[M * N];
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			a[i + j * M] = { 1,2,3 };
		}
	}
	delete[] a;
}

void draw2()
{
	Timer timer;
	Rgb** a = new Rgb * [M];
	for (int i = 0; i < M; i++)
	{
		a[i] = new Rgb[N];
		for (int j = 0; j < N; j++)
		{
			a[i][j] = { 1,2,3 };
		}
		delete[] a[i];	//注意这行
	}
	delete[] a;
}

int main()
{
	draw1();
	draw2();
	std::cin.get();
}

运行速度对比

delete[] a[i];
  • 在debug模式下注释掉draw()函数的这行代码
    在这里插入图片描述

  • 取消注释
    在这里插入图片描述

  • 在Release模式下注释代码
    在这里插入图片描述

  • Release模式下取消注释
    在这里插入图片描述

原文地址:https://www.cnblogs.com/chengmf/p/14893532.html