c++ - 为什么在堆上迭代一个大数组比在堆栈上迭代相同大小的数组更快?

标签 c++ visual-c++

我正在分配 2 个相同大小的数组,一个在堆栈上,一个在堆上,然后用简单的赋值遍历它们。

可执行文件被编译为主线程堆栈分配 40mb。

此代码仅经过测试可在带有/STACK:41943040 链接器标记的 vc++ 中编译。

#include "stdafx.h"
#include <string>
#include <iostream>
#include <malloc.h>
#include <windows.h>
#include <ctime>

using namespace std;

size_t stackavail()
{
    static unsigned StackPtr;   // top of stack ptr
    __asm mov [StackPtr],esp    // mov pointer to top of stack
    static MEMORY_BASIC_INFORMATION mbi;            // page range
    VirtualQuery((PVOID)StackPtr,&mbi,sizeof(mbi)); // get range
    return StackPtr-(unsigned)mbi.AllocationBase;   // subtract from top (stack grows downward on win)
}

int _tmain(int argc, _TCHAR* argv[])
{
    string input;

    cout << "Allocating 22mb on stack." << endl;
    unsigned int start = clock();
    char eathalfastack[23068672]; // approx 22mb
    auto length = sizeof(eathalfastack)/sizeof(char);
    cout << "Time taken in ms: " << clock()-start << endl;

    cout << "Setting through array." << endl;
    start = clock();
    for( int i = 0; i < length; i++ ){
        eathalfastack[i] = i;
    }
    cout << "Time taken in ms: " << clock()-start << endl;
    cout << "Free stack space: " << stackavail() << endl;


    cout << "Allocating 22mb on heap." << endl;
    start = clock();
    // auto* heaparr = new int[23068672]; // corrected
    auto* heaparr = new byte[23068672];
    cout << "Time taken in ms: " << clock()-start << endl;

    start = clock();
    cout << "Setting through array." << endl;
    for( int i = 0; i < length; i++ ){
        heaparr[i] = i;
    }
    cout << "Time taken in ms: " << clock()-start << endl;

    delete[] heaparr;
    getline(cin, input);
}

输出是这样的:

    Allocating 22mb on stack.
    Time taken in ms: 0
    Setting through array.
    Time taken in ms: 45
    Free stack space: 18872076
    Allocating 22mb on heap.
    Time taken in ms: 20
    Setting through array.
    Time taken in ms: 35

为什么堆栈数组的迭代比堆上的相同事物慢?

编辑: nneonneo 纠正我的错误

现在输出是相同的:

    Allocating 22mb on stack.
    Time taken in ms: 0
    Setting through array.
    Time taken in ms: 42
    Free stack space: 18871952
    Allocating 22mb on heap.
    Time taken in ms: 4
    Setting through array.
    Time taken in ms: 41

根据以下 Öö Tiib 的回答发布构建:

    Allocating 22mb on stack.
    Time taken in ms: 0
    Setting through array.
    Time taken in ms: 5
    Free stack space: 18873508
    Allocating 22mb on heap.
    Time taken in ms: 0
    Setting through array.
    Time taken in ms: 10

最佳答案

你们的数组大小不一样; sizeof(char[23068672]) != sizeof(int[23068672]),元素类型不同。

关于c++ - 为什么在堆上迭代一个大数组比在堆栈上迭代相同大小的数组更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907399/

相关文章:

c++ - 我应该写一个方法来转换距离还是一堆方法?

c++ - Scons:使用 Object builder 构建头文件

c++ - 我无法让我的程序正确读取输入文件中的值(二维数组)

c++ - 使用 OpenGL 绘制时钟的小时标记

c++ - 模板类中 `is_base_of` 的静态断言因 MSVC 中的意外类型而失败

c - VC++6.0上的Stdio.h

c++ - 放弃 CMainFrame 中的 ALT 按键操作

c# - P/Invoke - 结构参数的简单添加

c++ - 你能在 C++ 中隐藏虚方法吗?

c++ - 为什么一个 ctor 不能调用另一个 ctor 来初始化对象