Код: Выделить всё
#include
#include
#include
int main()
{
const auto t1 = std::chrono::high_resolution_clock::now();
/* When using loop #1, this code takes 80 milliseconds to run.
* When using loop #2, this code takes 120 milliseconds to run.
*/
for (size_t idx = 0; idx < 999999; idx ++)
{
std::string str = "Hello, World!";
#if 1
// loop #1
for (auto & c : str)
{
c = std::tolower(c);
}
#else
// loop #2
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c)
{
return std::tolower(c);
});
#endif
}
const auto t2 = std::chrono::high_resolution_clock::now();
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79580967/why-is-ranged-for-loop-faster-than-stdtransform[/url]
Мобильная версия