gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:47 24 4
gpt4 key购买 nike

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

但是,我从新建和删除中得到了奇怪的时间。在 500KB 比 510KB 和 500KB 之后的花费更多的时间。更进一步,500KB之后显示内存越大,耗时越少。

(是不是因为 Windows 将内存保持在“大” block 中。所以当分配超过 500KB 的内存时,Windows 更容易?那么,为什么是 500KB?)

那么,当数据大小小于500K时,Copy更好,否则使用指针?

有没有人对此有所了解?

下面是我得到的时间。取10000次运算后的平均值。

(环境: win7, vs2010)

新建和删除:

大小: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

复制:线性

大小: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;

}

最佳答案

(Is it because that Windows keep memory in "big" chunk. So that when allocating memory more than 500KB, it was easier for Windows? Then, why 500KB?)

500K 取决于实现。对于另一个系统或编译器,它可能会有所不同。

So, Copy is better when size of data below 500K, otherwise use pointer?

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

关于c++ - 在新建和删除操作中花费了意外的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com