Understanding volatile asm vs volatile variable
问题 We consider the following program, that is just timing a loop: #include <cstdlib> std::size_t count(std::size_t n) { #ifdef VOLATILEVAR volatile std::size_t i = 0; #else std::size_t i = 0; #endif while (i < n) { #ifdef VOLATILEASM asm volatile("": : :"memory"); #endif ++i; } return i; } int main(int argc, char* argv[]) { return count(argc > 1 ? std::atoll(argv[1]) : 1); } For readability, the version with both volatile variable and volatile asm reads as follow: #include <cstdlib> std::size_t