为什么在 while 循环中使用或不使用 sleep/usleep 时花费在相同函数/方法调用上的时间会有很大差异?

如何解决为什么在 while 循环中使用或不使用 sleep/usleep 时花费在相同函数/方法调用上的时间会有很大差异?

我原来的c++演示代码如下所示:

impl<T,U: From<T>> Into<U> for T;

有些人可能会怀疑我对 instance.Search() 的调用带来了未知的东西,所以请参考下面的代码:

From<i32> for StructA

前提:使用int counter = 0; while (counter < 5) { auto start = std::chrono::high_resolution_clock::now(); // instance and result are pre-defined local variables instance.Search(40.055948,116.411325,&result); auto end = std::chrono::high_resolution_clock::now(); int64_t cost_us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); std::cout << "cost_us=" << cost_us << std::endl; // usleep(100); // case1: sleep 100 us // sleep(1); // case2: sleep 1 second // case3: no sleep at all counter++; }s 统计cache_miss、指令等...

case 1:在while循环末尾添加usleep(100)时,进程结束后的结果为:

#include <time.h>
#include <unistd.h>
#include <iostream>
#include <set>
#include <chrono>

void test(const std::set<int>& numbers) {

  for (int counter = 0; counter < 5; ++counter) {
    auto start = std::chrono::high_resolution_clock::now();
    auto it = numbers.lower_bound(5555555);
    auto end = std::chrono::high_resolution_clock::now();
    int64_t cost_us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "cost_us=" << cost_us << std::endl;
    // usleep(100);
    // sleep(1);
  }
}

int main() {
  std::set<int> test_set;
  for (int i = 0; i < 100000000; i++) {
    test_set.insert(i);
  }
  test(test_set);
}

case 2: 添加 sleep(1) 时,结果为:

perf stat

情况 3:根本没有 sleep()/usleep(),结果是:

cost_us=5
cost_us=5
cost_us=5
cost_us=8
cost_us=6
Performance counter stats for './latency_perf_test_sleep_100_us':
    1,785,438     cache-references
      419,583     cache-misses        #   23.500 % of all cache refs
  203,832,235     cycles
  118,093,490     instructions        #   0.58 insn per cycle
   23,198,708     branches
       35,092     faults
          302     migrations
​
  1.031460583 seconds time elapsed


如上所述,相同函数/方法调用的时间成本在不同情况下差异很大。起初,我倾向于认为 cost_us=7 cost_us=65 cost_us=21 cost_us=21 cost_us=32 Performance counter stats for './latency_perf_test_sleep_1_sec': 15,302     cache-references     1,303,941     cache-misses          #   8.639 % of all cache refs 14,759,103,041    cycles 24,548,401,788    instructions         #   1.66 insn per cycle 5,062,488,529     branches        35,372     faults         3,444     migrations   6.033182248 seconds time elapsed 会导致缓存未命中(我的调用使用的数据)。但是,在我使用 cost_us=5 cost_us=2 cost_us=1 cost_us=1 cost_us=1 Performance counter stats for './latency_perf_test_without_sleep': 1,715,128 cache-references   420,368 cache-misses              #   24.509 % of all cache refs   209,238,013 cycles   130,647,626 instructions              #   0.62 insn per cycle    25,827,456 branches        35,092 faults           362 migrations   1.032256618 seconds time elapsed 将我的进程与特定的 cpu 内核绑定后,差异并没有像我预期的那样消失。

我也想知道为什么添加 sleep() / taskset 会导致 sleep() 计数的 usleep() 急剧增加。

我没有阅读任何 instructionsperf stat 的源代码,但我猜当进程调用 sleep()usleep() (两者都在内部调用 sleep())。

谁能解释这种奇怪现象背后的原因?提前致谢。

解决方法

谁能解释这种奇怪现象背后的原因?

ldd --version ldd (GNU libc) 2.17 版权所有 (C) 2012

您的 sleep 看起来像 this glibc sources from 2012 sysv/linux/sleep.c。由于内核错误(或者它是预期的 SysV 行为?我不确定......),它在调用 SIGCHLD 之前阻塞 nanosleep,请参阅 this commit 并由它的 LKML 线程引用。

很可能额外的开销来自调用 __sig* 相关函数。要进一步调查,请分析代码(考虑 gprof)或/并从带有调试信息的源编译您的 glibc(或仅安装调试信息,如果可以)然后分析代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-