今天在组员进行win程序跨平台开发linux程序的时候,编译中发现gui层面使用了vs平台的froeach函数,正确的修改为了C++14跨平台接口,因为大家常规使用的时候一般是for、或者iter等方式。针对std::for_each和iter、下标访问效率做了一下测试,代码如下:
// C++ 遍历容器效率问题.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <climits>
#include <string>
#include <time.h>
#include <chrono>
#include <stdarg.h>
using namespace std;
// 获取当前时间,最小单位ms,C++11实现,跨平台
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> TimePoint;
//////////////////////////////////////////////////////////////////////////
// 获取当前时间
TimePoint currentTime()
{
return std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
}
class CTest
{
static int nCount;
public:
void add(){ }
};
int CTest::nCount = 0;
///////////////////////////////////////////////////////////
//
int _tmain(int argc, _TCHAR* argv[])
{
typedef std::vector<CTest*> VEC;
VEC::size_type nAmount = 10000;
std::vector<CTest*> vtTests;
vtTests.resize(nAmount);
for (VEC::size_type i = 0; i < nAmount; ++i)
{
vtTests[i] = new CTest();
}
TimePoint dwStart, dwEnd;
// STL -- for_each
dwStart = currentTime();
std::for_each(vtTests.begin(), vtTests.end(), std::mem_fun<void, CTest>(&CTest::add));
dwEnd = currentTime();
std::cout << "for_each:\t" << (dwEnd - dwStart).count() << std::endl;
// STL -- iterator
dwStart = currentTime();
std::vector<CTest*>::iterator itr_begin = vtTests.begin();
std::vector<CTest*>::iterator itr_end = vtTests.end();
for (; itr_begin != itr_end; ++itr_begin)
{
(*itr_begin)->add();
}
dwEnd = currentTime();
std::cout << "iterator:\t" << (dwEnd - dwStart).count() << std::endl;
// STL -- iterator2
dwStart = currentTime();
std::vector<CTest*>::iterator itr_begin2 = vtTests.begin();
for (; itr_begin2 != vtTests.end(); ++itr_begin2)
{
(*itr_begin2)->add();
}
dwEnd = currentTime();
std::cout << "iterator:\t" << (dwEnd - dwStart).count() << std::endl;
// operator[]
dwStart = currentTime();
for (size_t i = 0; i < nAmount; ++i)
{
vtTests[i]->add();
}
dwEnd = currentTime();
std::cout << "operator:\t" << (dwEnd - dwStart).count() << std::endl;
system("pause");
return 0;
}
反汇编如下:
// STL -- for_each
dwStart = currentTime();
std::for_each(vtTests.begin(), vtTests.end(), std::mem_fun<void, CTest>(&CTest::add));
dwEnd = currentTime();
std::cout << "for_each:\t" << (dwEnd - dwStart).count() << std::endl;

// STL -- iterator
dwStart = currentTime();
std::vector<CTest*>::iterator itr_begin = vtTests.begin();
std::vector<CTest*>::iterator itr_end = vtTests.end();
for (; itr_begin != itr_end; ++itr_begin)
{
(*itr_begin)->add();
}
dwEnd = currentTime();
std::cout << "iterator:\t" << (dwEnd - dwStart).count() << std::endl;
反汇编代码如下:

// STL -- iterator2
dwStart = currentTime();
std::vector<CTest*>::iterator itr_begin2 = vtTests.begin();
for (; itr_begin2 != vtTests.end(); ++itr_begin2)
{
(*itr_begin2)->add();
}
dwEnd = currentTime();
std::cout << "iterator:\t" << (dwEnd - dwStart).count() << std::endl;

// operator[]
dwStart = currentTime();
for (size_t i = 0; i < nAmount; ++i)
{
vtTests[i]->add();
}
dwEnd = currentTime();
std::cout << "operator:\t" << (dwEnd - dwStart).count() << std::endl;

本文通过一个C++代码示例,对比了在跨平台开发中,使用STL的`for_each`函数与传统迭代器方式遍历容器的效率。实验结果显示了不同遍历方式在执行时间上的差异,为C++程序员在选择遍历方式时提供了参考。
5303

被折叠的 条评论
为什么被折叠?



