简体   繁体   English

使用clock_gettime()问题

[英]usleep with clock_gettime() issue

I was doing a microbenchmark. 我正在做一个微基准测试。 My code appears something like this 我的代码看起来像这样

while(some condition){
    struct timespec tps, tpe;
    clock_gettime(CLOCK_REALTIME, &tps);

    encrypt_data(some_data)

    clock_gettime(CLOCK_REALTIME, &tpe);
    long time_diff = tpe.tv_nsec - tps.tv_nsec;
    usleep(1000);
}

However, the sleep time that I put in usleep() actually affects the observed time_diff that I get. 然而,我放入usleep()的睡眠时间实际上影响了我得到的观察到的time_diff。 If I measure the execution of this code using the skeleton above the time I get varies from ~1.8us to ~7us for sleep time of 100us and 1000us respectively. 如果我使用上面的骨架测量这个代码的执行时间,我得到的时间从~1.8us变化到~7us,睡眠时间分别为100us和1000us。 Why would the measured time change with the change in sleep time, when sleep time is outside the instrumentation block? 当睡眠时间在仪表区之外时,为什么测量时间会随着睡眠时间的变化而变化?

The time results are average of multiple runs. 时间结果是多次运行的平均值。 I am using Ubuntu 14.04 to run this code. 我正在使用Ubuntu 14.04来运行此代码。 For encryption I am using aesgcm from openssl. 对于加密,我使用的是openssl的aesgcm。

I know that this is not the best way to microbenchmark but that is not the problem here. 我知道这不是microbenchmark的最佳方法,但这不是问题。

Did you disable the CPU scaling? 你是否禁用了CPU缩放?

sudo cpupower frequency-set --governor performance

See here and here . 看到这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM