c++11正则匹配整理

xingyun86 2020-12-25 913

sregex_iterator

sregex_token_iterator

regex_search

测试结果:当低于连续1000次时,时间消耗差别不大;当大于连续1000次时,regex_search表现优秀,比另外两个少花费30多ms。


try
    {
        DWORD64 start = 0;
        DWORD64 end = 0;
        int sum = 0;
        std::cout << std::endl << "------------------1---" << std::endl;
        sum = 0;
        for (int i = 0; i < 1000; i++)
        {
            start = GetTickCount64();
            for (std::sregex_iterator pos(str.cbegin(), str.cend(), pattern), end; pos != end; pos++)
            {
                //std::cout << ": " << pos->str(1) << std::endl;
            }
            end = GetTickCount64();
            std::cout << end - start << " ";
            sum += end - start;
        }
        std::cout << std::endl << "SUM1=" << sum << std::endl;
        std::cout << "------------------2---" << std::endl;
        sum = 0;
        for (int i = 0; i < 1000; i++)
        {
            start = GetTickCount64();
            for (std::sregex_token_iterator pos(str.cbegin(), str.cend(), pattern, 1), end; pos != end; pos++)
            {
                //std::cout << ": " << *pos << std::endl;
            }
            end = GetTickCount64();
            std::cout << end - start << " ";
            sum += end - start;
        }
        std::cout << std::endl << "SUM2=" << sum << std::endl;
        std::cout << "------------------3---" << std::endl;
        sum = 0;
        for (int i = 0; i < 1000; i++)
        {
            start = GetTickCount64();
            for (auto its = str.cbegin(), ite = str.cend(); std::regex_search(its, ite, sm, pattern) == true; its = sm.begin()->second)
            {
                //std::cout << ": " << sm[1] << std::endl;
            }
            end = GetTickCount64();
            std::cout << end - start << " ";
            sum += end - start;
        }
        std::cout << std::endl << "SUM3=" << sum << std::endl;
    }
    catch (const std::exception&e)
    {
        std::cout << e.what() << std::endl;
    }


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回