This is what happens when people forget what C++ is. Your source code is not a sequence of instructions for the computer chip to execute one at a time. It is a description of a program. It is an abstraction.
People like to talk about optimisation this and release mode that. People like to treat the optimiser as if it were an afterthought, tacked on at the end of your build process to what would otherwise be a line-by-line match to the statements you typed out. It's not. It's a fundamental part of what it means to take an abstract program and create something real from it (read: the process of compilation). Optimisation levels would be better named "effort levels": how much effort the compiler goes to by venturing further and further from your words in search of a real program that'll execute nice and quickly on a computer chip.
Your code, albeit in a very round-about way, describes a program that achieves nothing. So you should very well expect a decent compiler to produce an executable that does no meaningful work.
Sure, you can trick it with side-effects and blah blah blah and other stuff that you didn't actually need to measure in the first place, but that just misses the point. Instead, benchmark actual, useful work. Anything else is just wasting your own time!
If you need a delay in your program, use std::this_thread::sleep_for
.
It is different in Python because that is an interpreted scripting language, which does not [typically] go through any translation before being executed. That's why the book you're reading talks about Python, not C++. It is usually folly to take concepts from Python and try to apply them to C++.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…