#include <iostream>
#include <chrono>
#define NUM_ITERATIONS 10000000000ll
#define X_INC 17
#define Y_INC -31
int main()
{
// Block 1: one operation in loop body
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
for (long i = 0; i < NUM_ITERATIONS; i++) {x+=X_INC;}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x1 for: " << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
// Block 1b: one operation in loop body, while loop
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
while (x < (X_INC * NUM_ITERATIONS)) { x+=X_INC; }
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x1 while: " << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
// Block 1c: one operation in loop body, for loop, register increment
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
register long i;
for (i = 0; i < NUM_ITERATIONS; i++) { x+=X_INC; }
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x1 register-for: " << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
// Block 2: two operations in loop body
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
for (long i = 0; i < NUM_ITERATIONS; i++) {x+=X_INC; y+=Y_INC;}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x2 for: " << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
// Block 2b: two operations in loop body, while loop
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
while (x < (X_INC * NUM_ITERATIONS)) { x+=X_INC; y+=Y_INC; }
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x2 while: " << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
// Block 2c: one operation in loop body, for loop, register increment
{
int64_t x = 0, y = 0;
auto start = std::chrono::high_resolution_clock::now();
register long i;
for (i = 0; i < NUM_ITERATIONS; i++) { x+=X_INC; y+=Y_INC; }
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "x2 register-for :" << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
}
return 0;
}