【genius_platform软件平台开发】第四十一讲:for_each、迭代器和下标访问效率问题

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

今天在组员进行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;

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隨意的風

如果你觉得有帮助,期待你的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值