Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created March 16, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxdot/b70aca82b5ce665d3906 to your computer and use it in GitHub Desktop.
Save boxdot/b70aca82b5ce665d3906 to your computer and use it in GitHub Desktop.
#include <array>
#include <iostream>
#include <stdio.h>
#include <chrono>
enum class Ax { X = 0, Y = 1, Z = 2 };
static constexpr std::array<Ax, 3> AXES = {Ax::X, Ax::Y, Ax::Z};
static constexpr auto COUNT = 200000000;
void experiment1() {
int res = 0;
auto start = std::chrono::steady_clock::now();
for (size_t i = 0; i < COUNT; ++i) {
for (auto ax : AXES) {
res += static_cast<int>(i);
}
}
auto end = std::chrono::steady_clock::now();
std::cerr
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start).count() << std::endl;
}
void experiment2() {
int res = 0;
auto start = std::chrono::steady_clock::now();
for (size_t i = 0; i < COUNT; ++i) {
for (int j = 0; j < 3; ++j) {
res += j;
}
}
auto end = std::chrono::steady_clock::now();
std::cerr
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start).count() << std::endl;
}
int main(int argc, char const *argv[])
{
experiment1();
experiment2();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment