是for循环还是迭代器是遍历字符串的更快方法? [英] Is a for-loop or iterator a faster way to traverse a string?

查看:353
本文介绍了是for循环还是迭代器是遍历字符串的更快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串。

std::string strLine;

我可以使用遍历字符串char,如果循环

for (int i = 0; strLine[i]; i++)

我可以做的另一种方法是使用字符串迭代器

Another way i can do is using string iterator

string::iterator it;
for (it = strLine.begin(); it < strLine.end(); it++) 

哪种迭代方式更快捷,更灵活?

Which is a faster and flexible way to iterate ?

推荐答案

查看此代码,打印字符串这是一个字符串!
实时

Check out this code printing a string This is a string!: LIVE

    cpu_time_start = clock();
    real_time_start = chrono::high_resolution_clock::now();

    for (auto i = 0u; i < strLine.size(); ++i)
        cout << strLine[i];
    cout << endl;

    cpu_time_end = clock();
    real_time_end = chrono::high_resolution_clock::now();
    cout << "index-based loop CPU time: " << 1000.0 * (cpu_time_end-cpu_time_start) / CLOCKS_PER_SEC << " ms\n"
         << "index-based loop real time: "<< chrono::duration<double, milli>(real_time_end-real_time_start).count() << " ms\n";

    //---------------------------------------------------------------------

    // get start time, same as above 
    for (auto i = strLine.begin(); i != strLine.end(); ++i)
        cout << *i;
    cout << endl;
    // get end time and print 

    //---------------------------------------------------------------------

    // get start time, same as above
    for (auto &c: strLine)
        cout << c;
    cout << endl;
    // get end time and print 






编辑:

感谢@cigien,他指出了通过预热更准确的基准测试方法,看来它们是一种就执行时间而言,性能在某些方面彼此接近,并且如果您更改代码中的循环顺序,则似乎任何一个都可能比另一个稍快,我认为这是由于缓存,但是我不要以为编译器会为他们生成相同的代码。您可以通过先打印出字符串然后进行迭代打印来预热。比较它们的一种更准确的方法可能是自己测试一个程序中的每个循环。

Thanks to @cigien, He pointed out to a more accurate way for benchmarking by warming up, It seems that they are a little bit close to each other in performance in terms of the time of execution, and if you change the order of loops in the code, it appears that any could be slightly faster than the other, I think that's due to caching, but yet I don't think that the compiler will produce the same code for them. You can warm up by just printing out the string first before printing by iteration. And maybe a more accurate way to compare them is to test each loop in one program on its own.

这是用 g ++编译时的输出-Wall -std = c ++ 17 -O2

warming up for tests ...
This is a string!
warmed up for tests ...
This is a string!
index-based loop CPU time: 0.008 ms
index-based loop real time: 0.005986 ms
This is a string!
iterator-based loop CPU time: 0.004 ms
iterator-based loop real time: 0.003417 ms
This is a string!
range-for based loop CPU time: 0.003 ms
range-for based loop real time: 0.002755 ms

我将保留旧答案的这一部分,让人们知道发生了什么事情:

I'll leave this part of the OLD ANSWER for people to know what happened:

OLD输出!

This is a string!
index-based loop CPU time: 0.054 ms
index-based loop real time: 0.054416 ms
This is a string!
iterator-based loop CPU time: 0.005 ms
iterator-based loop real time: 0.004291 ms
This is a string!
range-for based loop CPU time: 0.004 ms
range-for based loop real time: 0.004308 ms

看起来范围循环基于迭代器的循环非常接近就执行时间而言,两者的执行速度均比基于索引的循环快约 10倍。尝试使用更大长度的字符串以获得更准确的结果,然后运行多次并取平均值。

It looks that the range-for loop and the iterator-based loop are very close in performance in terms of the time of execution and both execute about 10x faster than the index-based loop. Try larger length strings for more accurate results and run it many times and take the average.

您也可以尝试在编译器资源管理器,我认为不会生成相同的代码。

You could also try compiling it on Compiler Explorer, I don't think the same code will be generated.

这篇关于是for循环还是迭代器是遍历字符串的更快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆