比较array、vertor和数组的效率问题

博客通过测试数据指出,STD::ARRAY在性能上优于STD::vector和原生数组,尤其在避免使用for (auto &v : vec)迭代方式时,vector性能下降显著。原生数组的耗时表现稳定。
源代码如下:
#include "stdafx.h"
#include <iostream>
#include <chrono>
#include <vector>
#include <array>
#include <iostream>
#include <windows.h>
using namespace std;




#define LOOP 100 * 1000UL
#define TIMES 10 * 1000UL
//typedef  double ARR_TYPE;
typedef  unsigned int ARR_TYPE;





array<ARR_TYPE, LOOP> stdarr = {0};
vector<ARR_TYPE> vec(LOOP);
ARR_TYPE arr[LOOP];
long size = 0;




int test()
{
 DWORD dwTickstart = 0;
 DWORD dwTickend = 0;





 size = stdarr.size();
 dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
    for (unsigned long i = 0; i != size; i++) { stdarr[i] = k; }  // for
 dwTickend = GetTickCount();
 cout << "STD::ARRAY: " << dwTickend-dwTickstart << " microseconds." << endl;





 dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
 for (auto &v : stdarr)
 {
  v = k; 
 }
 dwTickend = GetTickCount();
 cout << "STD::ARRAY: auto:" << dwTickend-dwTickstart << " microseconds." << endl;




 dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
 for (auto &v : stdarr)
 {
  v = k;
 }
 dwTickend = GetTickCount();
 cout << "STD::ARRAY: auto:" << dwTickend-dwTickstart << " microseconds." << endl;










 //////////////////////////////////////////////////////////////////////////////
    
    dwTickstart = GetTickCount();
 size = vec.size();




 for (int k = 0; k < TIMES; k++)
    for (unsigned long i = 0; i != size; i++) { vec[i] = i; }  // for
 dwTickend = GetTickCount();
 cout << "STD::vector:" << dwTickend-dwTickstart << " microseconds." << endl;




 dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
 for (auto &v : vec)
 {
  //cout << v << endl;
  v = k;
 }
 dwTickend = GetTickCount();
 cout << "STD::vector: auto:" << dwTickend-dwTickstart << " microseconds." << endl;





 //////////////////////////////////////////////////////////////////////////////










 ARR_TYPE arr2[LOOP];
 int * aaaaa = (int *)arr2;
    dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
    for (unsigned int i = 0; i != LOOP; i++) { 
  aaaaa[i] = i;  
  //cout << aaaaa[i] << endl;
 }
    dwTickend = GetTickCount();
    cout << "ARRAY: " << dwTickend-dwTickstart << " microseconds." << endl;




 
    dwTickstart = GetTickCount();
 for (int k = 0; k < TIMES; k++)
    for (unsigned int i = 0; i != LOOP; i++) { arr[i] = i - 1; }
    dwTickend = GetTickCount();
    cout << "ARRAY: " << dwTickend-dwTickstart << " microseconds." << endl;





    return 0;




}




int _tmain(int argc, _TCHAR* argv[])
{
 test();
 getchar();
 return 0;
}

测试结果数据如下:

STD::ARRAY: 94 microseconds.
STD::ARRAY: auto:94 microseconds.
STD::ARRAY: auto:94 microseconds.
STD::vector:172 microseconds.
STD::vector: auto:390 microseconds.
ARRAY: 172 microseconds.
ARRAY: 172 microseconds.

1、以上三种方式中,使用STD::ARRAY时,耗时是最少的,

2、使用vector的时候,切记不要使用for (auto &v : vec)的方式,可能会造成大量的耗时。

3、原生数组耗时比较稳定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值