当前位置: 首页>>代码示例>>C++>>正文


C++ StopWatch::GetSeconds方法代码示例

本文整理汇总了C++中StopWatch::GetSeconds方法的典型用法代码示例。如果您正苦于以下问题:C++ StopWatch::GetSeconds方法的具体用法?C++ StopWatch::GetSeconds怎么用?C++ StopWatch::GetSeconds使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StopWatch的用法示例。


在下文中一共展示了StopWatch::GetSeconds方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: TestPerformance

// test code
void TestPerformance(const No2StringBuilder<wchar_t> &tested, const std::vector<std::wstring> &tested2) {
    const int loops = 4001; // Odd number to keep 1 result at the end.
    ////////////////
    // Test std::accumulate()
    ////////////////
    std::wstring accumulate_result;
    size_t accumulate_len = 0;
    StopWatch swAccumulate;
    for (int i = 0; i < loops; ++i) {
        std::wstring accumulator;
        accumulate_result = std::accumulate(tested2.begin(), tested2.end(), accumulator);
        accumulate_len ^= accumulate_result.size();
    }
    swAccumulate.Stop();
    using std::cout;
    using std::endl;
    cout << "\tstd::accumulate: " << swAccumulate.GetSeconds() << ": size " << accumulate_len << endl;
    ////////////////
    // Test ToString()
    ////////////////
    std::wstring toString_result;
    size_t toString_len = 0;
    StopWatch swToString;
    for (int i = 0; i < loops; ++i) {
        toString_result = tested.toString();
        toString_len ^= toString_result.size();
    }
    swToString.Stop();
    cout << "\tToString: " << swToString.GetSeconds() << ": size " << toString_len << endl;
    ////////////////
    // Test join()
    ////////////////
    std::wstring join_result;
    size_t join_len = 0;
    StopWatch swJoin;
    for (int i = 0; i < loops; ++i) {
        join_result = tested.join(L",");
        join_len ^= join_result.size();
    }
    swJoin.Stop();
    cout << "\tJoin: " << swJoin.GetSeconds() << ": size " << join_len << endl;
    
    ////////////////
    // basic_ostringstream<wchar_t>
    // First test: only getting the string, not filling the stream.
    ////////////////
    typedef std::basic_ostringstream<wchar_t> wostringstream;
    wostringstream woss;
    for(size_t j = 0; j < tested2.size(); ++j)
        woss << tested2[j];
    std::wstring stringStream_result;
    size_t stringStream_len = 0;
    StopWatch swStringStream;
    for (int i = 0; i < loops; ++i) {
        stringStream_result = woss.str();
        stringStream_len ^= stringStream_result.size();
    }
    swStringStream.Stop();
    cout << "\tostringstream: " << swStringStream.GetSeconds() << ": size " << stringStream_len << endl;
    
    ////////////////
    // Test lambda
    ////////////////
    
    std::wstring strAccumulator;
    size_t lambdaResult_len = 0;
    StopWatch swLambda;
    for (int i = 0; i < loops; ++i) {
        std::wstring strtmp;
        std::for_each(tested2.begin(), tested2.end(), [&](const std::wstring &s){ strtmp += s; });
        if (0 == i) {
            strAccumulator = strtmp;
        }
        lambdaResult_len ^= strtmp.size();
    }
    swLambda.Stop();
    cout << "\tLambda: " << swLambda.GetSeconds() << ": size " << lambdaResult_len << endl;
    
    ////////////////
    // Show results so far.
    ////////////////
    cout << "* Performance test:" << endl
    << "    Accumulate took " << swAccumulate.GetSeconds() << " seconds, and ToString() took " << swToString.GetSeconds() << " seconds." << endl
    << "    The relative speed improvement was " << ((swAccumulate.GetSeconds() / swToString.GetSeconds()) - 1) * 100 << "%" << endl
    << "    std::ostringstream took " << swStringStream.GetSeconds() << " with a relative speed improvement of "  << ((swAccumulate.GetSeconds() / swStringStream.GetSeconds()) - 1) * 100 << "% (winning, you say?)" << endl
    << "    lambda took " << swLambda.GetSeconds() << " with a relative speed improvement of "  << ((swAccumulate.GetSeconds() / swLambda.GetSeconds()) - 1) * 100 << "% (winning, you say?)" << endl
    << "    Join took " << swJoin.GetSeconds() << " seconds."
    << endl;
    
    ////////////////
    // basic_ostringstream<wchar_t> and tostring: fill and execute.
    ////////////////
    
    if (true) {
        size_t tmp = 0;
        typedef std::basic_ostringstream<wchar_t> wostringstream;
        StopWatch swStringStream2;
        for (int i = 0; i < loops; ++i) {
            wostringstream woss;
//.........这里部分代码省略.........
开发者ID:JunAFa,项目名称:HighPerformanceCharacterString,代码行数:101,代码来源:main.cpp


注:本文中的StopWatch::GetSeconds方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。