View Issue Details

IDProjectCategoryView StatusLast Update
0016600CentOS-7glibcpublic2019-10-21 00:42
Reporterek1010 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version7.7-1908 
Target VersionFixed in Version 
Summary0016600: chrono call is much slower than clock_gettime
DescriptionCentOS 7 appears to have std::chrono calls as much slower than gettimeofday or clock_gettime direct calls.

$ ./a.out 100000
loops: 100000
getTimeOfDay(): 4 milliseconds
chrono: 49 milliseconds
clock_gettime(CLOCK_REALTIME): 3 milliseconds
clock_gettime(CLOCK_MONOTONIC): 3 milliseconds
Steps To ReproduceSample program to reproduce:
#include <iostream>
#include <unistd.h> /* POSIX flags */
#include <time.h> /* clock_gettime(), time() */
#include <sys/time.h> /* gethrtime(), gettimeofday() */
#include <sys/resource.h> /*cputime*/
#include <sys/times.h> /*cputime*/
#include <chrono>

using namespace std;

double getTimeOfDay() {
        /* POSIX. --------------------------------------------------- */
        struct timeval tm;
        gettimeofday(&tm, NULL);
        return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
}

int main(int argc, char *argv[]) {
        double wallTimeMultiplier = 1000000.0;

        int N = 10000;
        if(argc > 1) {
            N = atoi(argv[1]);
        }
        std::cout << "loops: " << N << std::endl;

        auto start = std::chrono::high_resolution_clock::now();
        for(int i = 0; i < N; ++i) {
                double x = getTimeOfDay();
                std::chrono::microseconds microseconds((long long)(x * wallTimeMultiplier));
        }
        auto end = std::chrono::high_resolution_clock::now();
        auto dt = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "getTimeOfDay(): " << dt.count() << " milliseconds" << std::endl;

        start = std::chrono::high_resolution_clock::now();
        for(int i = 0; i < N; ++i) {
                auto now = std::chrono::high_resolution_clock::now();
        }
        end = std::chrono::high_resolution_clock::now();
        dt = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "chrono: " << dt.count() << " milliseconds" << std::endl;

        start = std::chrono::high_resolution_clock::now();
        for(int i = 0; i < N; ++i) {
                struct timespec tv;
                clock_gettime(CLOCK_REALTIME, &tv);
                double thistime = (long) tv.tv_sec * 1000000000 + tv.tv_nsec;
        }
        end = std::chrono::high_resolution_clock::now();
        dt = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "clock_gettime(CLOCK_REALTIME): " << dt.count() << " milliseconds" << std::endl;

        start = std::chrono::high_resolution_clock::now();
        for(int i = 0; i < N; ++i) {
                struct timespec tv;
                clock_gettime(CLOCK_MONOTONIC, &tv);
                double thistime = (long) tv.tv_sec * 1000000000 + tv.tv_nsec;
        }
        end = std::chrono::high_resolution_clock::now();
        dt = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "clock_gettime(CLOCK_MONOTONIC): " << dt.count() << " milliseconds" << std::endl;

        return 0;
}
TagsNo tags attached.
abrt_hash
URL

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2019-10-16 00:18 ek1010 New Issue