1

我对 STL 容器有一个非常基本的疑问。我的要求是我想以多维数组的形式存储双精度值。我将直接对它们执行各种代数运算,即

myvector[4] = myvector[3] - 2 * myvector[2];

为此,我正在使用 for 循环和使用 [] 运算符进行迭代。我没有使用 STL 迭代器。我在这里找到了 2 种基本方法。我更喜欢速度而不是内存效率。由于我经常访问这些变量,我认为向量对我来说会很慢。那么你对这件事有什么拙见呢?我知道答案将基于您以前的经验,这就是我问这个问题的原因。如果这个问题太基本而无法在这里讨论,我很抱歉。

4

1 回答 1

4

您提供的链接列出了 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 等价于原始指针。但是一维比二维快得多。

于 2012-11-04T13:31:10.753 回答