// from https://gist.github.com/teknoraver/822116c7b1ad616d6c3663e0508f6841 // ctx_time Copyright (C) 2018 Matteo Croce // a tool measure the context switch time in nanoseconds #include #include #include #include #include #include #include int main() { const unsigned N = 1'000'000, W = 5; std::chrono::duration d[N]; for (auto& i: d) { auto start = std::chrono::high_resolution_clock::now(); syscall(-1L); i = std::chrono::high_resolution_clock::now() - start; } std::nth_element(d, d + N / 2, d + N); std::cout << "context switch time..." << std::endl << "\tminimum: " << std::setw(W) << std::min_element(d, d + N)->count() << "ns" << std::endl << "\tmedian : " << std::setw(W) << d[N / 2].count() << "ns" << std::endl << "\tmaximun: " << std::setw(W) << std::max_element(d, d + N)->count() << "ns" << std::endl; }