//----------------------------------------------------------------------------- // struct.cc //----------------------------------------------------------------------------- #include #include #include #include #include #include #include #include //----------------------------------------------------------------------------- const int N = 10000, REP = 1000; //----------------------------------------------------------------------------- std::random_device device; std::default_random_engine generator(device()); std::uniform_int_distribution distribution(0, 9); auto rng = std::bind(distribution, generator); //----------------------------------------------------------------------------- struct S { int a, b; S(): a(rng()), b(rng()) {} } const s[N]; int R[REP + 1]; // because... for (int ii = 1; ii <= REP; ++ii) //----------------------------------------------------------------------------- void v0() { for (int ii = 1; ii <= REP; ++ii) { int X1 = 0, X2 = 0; for(int i = 0; i < N; ++i) X1 += 2 * s[i].a + ii; for(int i = 0; i < N; ++i) X2 += 3 * s[i].b - ii; if (X1 < X2) R[ii] = X1; else R[ii] = X2; } } //----------------------------------------------------------------------------- void v1() { } //----------------------------------------------------------------------------- void v2() { } //----------------------------------------------------------------------------- void v3() { } //----------------------------------------------------------------------------- void v4() { } //----------------------------------------------------------------------------- void v5() { } //----------------------------------------------------------------------------- void v6() { } //----------------------------------------------------------------------------- void v7() { } //--------------------------------------------------------------------- template void test(F& f, const char* name) { const unsigned Z = 25; std::chrono::duration rep[Z]; std::fill(R, R + REP, 0); for (auto& i: rep) { auto start = std::chrono::high_resolution_clock::now(); f(); auto stop = std::chrono::high_resolution_clock::now(); i = stop - start; } std::nth_element(std::begin(rep), std::begin(rep) + Z / 2, std::end(rep)); std::cout << name << ':' << " result: " << std::accumulate(R + 1, R + REP + 1, 0) << " min: " << std::fixed << std::setprecision(2) << std::setw(10) << std::min_element(std::begin(rep), std::end(rep))->count() << "us" << " median: " << std::fixed << std::setprecision(2) << std::setw(10) << rep[Z / 2].count() << "us" << std::endl; } //----------------------------------------------------------------------------- int main() { test(v0, "v0"); test(v1, "v1"); test(v2, "v2"); test(v3, "v3"); test(v4, "v4"); test(v5, "v5"); test(v6, "v6"); test(v7, "v7"); } //-----------------------------------------------------------------------------