I am curious about the items below in for loop.
for(auto) vs for(auto &)
Separating the for loop
for(auto &) vs for(const auto &)
for(int : list) vs for(auto : list) [list is integer vector]
So, I wrote the below code for testing in the C++17 version.
It looks like seems difference in CMake debug mode(without optimization)
// In debug mode
1. elapsed: 7639 (1663305922550 - 1663305914911)
2. elapsed: 3841 (1663305926391 - 1663305922550)
3. elapsed: 3810 (1663305930201 - 1663305926391)
But in release mode(with gcc -O3) there is no difference between 1 ~ 3
// release mode
1. elapsed: 0 (1663305408984 - 1663305408984)
2. elapsed: 0 (1663305409984 - 1663305409984)
3. elapsed: 0 (1663305410984 - 1663305410984)
I don't know if my test method is wrong, Or is it correct that there is no difference depending on the optimization status?
Here is my testing source code.
// create test vector
const uint64_t max_ = 499999999; // 499,999,999
std::vector<int> v;
for (int i = 1; i < max_; i++)
v.push_back(i);
// test 1.
auto start1 = getTick();
for (auto& e : v)
{
auto t = e + 100; t += 300;
}
for (auto& e : v)
{
auto t = e + 200; t += 300;
}
auto end1 = getTick();
// test 2.
// Omit tick function
for (auto& e : v)
{
auto t1 = e + 100; t1 += 300;
auto t2 = e + 200; t2 += 300;
}
// test 3.
for (auto e : v)
{
auto t1 = e + 100; t1 += 300;
auto t2 = e + 200; t2 += 300;
}
...
And then, getTick() was obtained through chrono milliseconds.
uint64_t getTick()
{
return (duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());
}
Also, this testing progressed on Debian aarch64
- Jetson Xavier NX (jetpack 4.6, ubuntu 18.04LTS)
- 8Gb RAM
- GCC 7.5.0
Please advise if there is anything wrong. Thank you!