在新操作和删除操作中花费了意外的时间

时间:2017-03-21 04:32:18

标签: c++ windows visual-studio memory

我想知道哪种方式更适合在函数之间传递一些数据,通过复制或传递指针(new和delete)。所以,我测试了在new和delete操作上花费的时间和复制。复制时间线性增加,这是预期的。

但是,我从新的和删除中获得了奇怪的时间。 500KB花了比510KB更多的时间和500KB以后的时间。此外,500KB之后显示内存越大,花费的时间越少。

(是因为Windows将内存保留在“大”块中。因此,当分配内存超过500KB时,Windows更容易?那么,为什么500KB?)

因此,当数据大小低于500K时,复制效果更好,否则使用指针?

任何人都对此有所了解?

下面是我得到的时间。 10000次操作后需要平均。

(环境:win7,vs2010)

new并删除:

尺寸:10K NewDleteAverageTime:0.012293ms
规模:20K NewDleteAverageTime:0.027168ms
尺寸:30K NewDleteAverageTime:0.0502781ms
规模:40K NewDleteAverageTime:0.0739503ms
...
规模:210K NewDleteAverageTime:0.116843ms
规模:220K NewDleteAverageTime:0.117263ms
尺寸:230K NewDleteAverageTime:0.0960635ms
...
规模:430K NewDleteAverageTime:0.165539ms
规模:440K NewDleteAverageTime:0.162156ms
规模:450K NewDleteAverageTime:0.126388ms
规模:460K NewDleteAverageTime:0.168734ms
大小:470K NewDleteAverageTime:0.196589ms
规模:480K NewDleteAverageTime:0.25601ms
尺寸:490K NewDleteAverageTime:0.25485ms
规模:500K NewDleteAverageTime:0.264911ms
规模:510K NewDleteAverageTime:0.00591305ms
大小:520K NewDleteAverageTime:0.00230184ms

大小:530K NewDleteAverageTime:0.00215912ms
规模:540K NewDleteAverageTime:0.00219284ms
...
规模:980K NewDleteAverageTime:0.00241015ms
规模:990K NewDleteAverageTime:0.00244989ms
尺寸:1000K NewDleteAverageTime:0.0024912ms
规模:10M NewDleteAverageTime:0.00311146ms
规模:20M NewDleteAverageTime:0.00297647ms
规模:30M NewDleteAverageTime:0.00302636ms
规模:40M NewDleteAverageTime:0.00310456ms
规模:50M NewDleteAverageTime:0.00311733ms
规模:60M NewDleteAverageTime:0.00312078ms
规模:70M NewDleteAverageTime:0.0032505ms
大小:80M NewDleteAverageTime:0.00322513ms
大小:90M NewDleteAverageTime:0.00328945ms
大小:100M NewDleteAverageTime:0.00330236ms

copy:linearly

尺寸:110K CopyAverageTime:0.0128748ms
...
大小:480K CopyAverageTime:0.0286189ms
规模:490K CopyAverageTime:0.0261801ms
大小:500K CopyAverageTime:0.0295913ms
规模:510K CopyAverageTime:0.0308001ms
规模:520K CopyAverageTime:0.0308093ms
大小:530K CopyAverageTime:0.0306727ms
...
大小:1010K CopyAverageTime:0.0804052ms
...
大小:1810K CopyAverageTime:0.183536ms
...
大小:3710K CopyAverageTime:0.400298ms
...
大小:10M CopyAverageTime:1.80764ms
...
大小:60M CopyAverageTime:10.7776ms

这是我的代码

void NewDeleteTime( Unit U )
{
    ofstream out("NewDeleteTime.txt", ios::out | ios::app);
    string strUnit = Unit_M == U ? "M":"K";
    int UnitSize = Unit_M == U ? M1 :K1;

    const int interval = 10;
    const int MaxSize = 1001;
    const int repeat = 1000;

    vector<char*> vecData;
    vecData.reserve(10);

    TimerCounter tmr;

    int max = Unit_M == U ? 101 : 1001;

    for (int size = 10; size < max; size += interval)
    {
        // setSrcData( size * M1);

        tmr.Start();

        for ( int times = 0; times < repeat; ++ times )
        {
            // new
            for ( int i =0; i < 10; ++i )
            {
                char* pD = new char[size * UnitSize];
                vecData.push_back( pD );
            }


            // delete
            for ( int i =0; i < vecData.size(); ++i )
            {
                delete[] vecData[i];
            }

            vecData.clear();
        }

        tmr.Stop();

        cout<<"size: "<<size<<strUnit<<" NewDleteAverageTime:"<< tmr.dbTime * 1000 / repeat / 10 <<"ms"<<endl;
        out<<"size: "<<size<<strUnit<<" NewDleteAverageTime:"<< tmr.dbTime * 1000 / repeat / 10 <<"ms"<<endl;

    }

1 个答案:

答案 0 :(得分:0)

  

(是因为Windows将内存保留在&#34; big&#34; chunk。所以当分配内存超过500KB时,Windows更容易?那么,为什么500KB?)

500K依赖于实现。它可能与另一个系统或编译器不同。

  

因此,当数据大小低于500K时,复制效果更好,否则使用指针?

经验法则 - 不要做任何你不必做的事情。 即使复制和分配相对便宜,最好使用指针来获取足够大的数据(大于16-32字节,你的里程可能会有所不同)。