您提供的链接列出了 2 种创建“真实”二维数组的方法。一般来说,二维数组效率不高,因为它们需要大量分配。相反,您可以使用伪造的二维数组:
// Array of length L and width W
type* array1 = new type[L * W]; // raw pointers
std::vector<type> array2(L * W); // STL Vector
// Accessing a value. You have to use a convention for indices, and follow it.
// Here the convention is: lines are contiguous (index = x + y * W)
type value = array[x + y * W]; // raw pointer array & vector
这是一个简单的基准测试(仅限 Windows,除非您更改计时器部分):
#include <vector>
#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
typedef LARGE_INTEGER clock_int;
void start_timer(clock_int& v)
{
QueryPerformanceCounter(&v);
}
void end_timer(clock_int v, const char* str)
{
clock_int e;
QueryPerformanceCounter(&e);
clock_int freq;
QueryPerformanceFrequency(&freq);
std::cout << str << 1000.0 * ((double)(e.QuadPart-v.QuadPart) / freq.QuadPart) << " ms\n";
}
void test_2d_vector(unsigned int w, unsigned int h)
{
std::vector<std::vector<double> > a;
a.resize(h);
for(unsigned int t = 0; t < h; t++)
a[t].resize(w);
clock_int clock;
start_timer(clock);
// Benchmark random write access
for(unsigned int t = 0; t < w * h; t++)
a[rand() % h][rand() % w] = 0.0f;
end_timer(clock,"[2D] Random write (STL) : ");
start_timer(clock);
// Benchmark contiguous write access
for(unsigned int y = 0; y < h; y++)
for(unsigned int x = 0; x < w; x++)
a[y][x] = 0.0f;
end_timer(clock,"[2D] Contiguous write (STL) : ");
}
void test_2d_raw(unsigned int w, unsigned int h)
{
double** a = new double*[h];
for(unsigned int t = 0; t < h; t++)
a[t] = new double[w];
clock_int clock;
start_timer(clock);
// Benchmark random write access
for(unsigned int t = 0; t < w * h; t++)
a[rand() % h][rand() % w] = 0.0f;
end_timer(clock,"[2D] Random write (RAW) : ");
start_timer(clock);
// Benchmark contiguous write access
for(unsigned int y = 0; y < h; y++)
for(unsigned int x = 0; x < w; x++)
a[y][x] = 0.0f;
end_timer(clock,"[2D] Contiguous write (RAW) : ");
}
void test_1d_raw(unsigned int w, unsigned int h)
{
double* a = new double[h * w];
clock_int clock;
start_timer(clock);
// Benchmark random write access
for(unsigned int t = 0; t < w * h; t++)
a[(rand() % h) * w + (rand() % w)] = 0.0f;
end_timer(clock,"[1D] Random write (RAW) : ");
start_timer(clock);
// Benchmark contiguous write access
for(unsigned int y = 0; y < h; y++)
for(unsigned int x = 0; x < w; x++)
a[x + y * w] = 0.0f;
end_timer(clock,"[1D] Contiguous write (RAW) : ");
}
void test_1d_vector(unsigned int w, unsigned int h)
{
std::vector<double> a(h * w);
clock_int clock;
start_timer(clock);
// Benchmark random write access
for(unsigned int t = 0; t < w * h; t++)
a[(rand() % h) * w + (rand() % w)] = 0.0f;
end_timer(clock,"[1D] Random write (STL) : ");
start_timer(clock);
// Benchmark contiguous write access
for(unsigned int y = 0; y < h; y++)
for(unsigned int x = 0; x < w; x++)
a[x + y * w] = 0.0f;
end_timer(clock,"[1D] Contiguous write (STL) : ");
}
int main()
{
int w=1000,h=1000;
test_2d_vector(w,h);
test_2d_raw(w,h);
test_1d_vector(w,h);
test_1d_raw(w,h);
system("pause");
return 0;
}
使用 msvc2010 编译,发布 /Ox /Ot,它为我输出(Win7 x64,Intel Core i7 2600K):
[2D] Random write (STL) : 32.3436 ms
[2D] Contiguous write (STL) : 0.480035 ms
[2D] Random write (RAW) : 32.3477 ms
[2D] Contiguous write (RAW) : 0.688771 ms
[1D] Random write (STL) : 32.1296 ms
[1D] Contiguous write (STL) : 0.23534 ms
[1D] Random write (RAW) : 32.883 ms
[1D] Contiguous write (RAW) : 0.220138 ms
可以看到 STL 等价于原始指针。但是一维比二维快得多。