简体   繁体   English

在C ++中重复计时void函数

[英]Repeated timing of a void function in C++

I am trying to time a void function 我想尝试一个无效函数

for (size_t round = 0; round < 5; round++) {
    cpu_time_start = get_cpu_time();
    wall_time_start = get_wall_time();

    scan.assign_clusters(epsilon, mu);

    cpu_time_end = get_cpu_time();
    wall_time_end = get_wall_time();
    ...
}

The first timing yields 300 seconds, while the next four timings yields 0.000002 seconds. 第一个时间产生300秒,而接下来的四个时间产生0.000002秒。 This indicates that the void function call to assign_clusters is optimized out. 这表明对assign_clusters的void函数调用已经过优化。 How can I force my program to execute this time consuming function call every time, and yet still use optimization for the rest of the code? 如何强制我的程序每次执行这个耗时的函数调用,但仍然使用优化代码的其余部分?

What I usually do is to save the result of the function in question and then print it, but since this is a void function, do I have the same option? 我通常做的是保存有问题的函数的结果然后打印它,但由于这是一个void函数,我有相同的选项吗?

I use the following optimization flags: -std=c++0x -march=native -O2 我使用以下优化标志: -std=c++0x -march=native -O2

It depends on what is taking the time, to make the fix. 这取决于花时间做什么来修复。

This could be caused by :- 这可能是由以下原因引起的: -

  1. Loading services. 加载服务。 Your clustering may be database based, and requires the database services to start (the first time) 您的群集可能是基于数据库的,并且需要启动数据库服务(第一次)
  2. Disk caching. 磁盘缓存。 The OS will remember data it has read, and be able to provide the data as if it was in memory. 操作系统将记住它已读取的数据,并能够像在内存中一样提供数据。
  3. Memory caching. 内存缓存。 The CPU has different speeds of memory available to it, using the same memory twice, would be faster the second time. CPU具有不同的可用内存速度,使用相同的内存两次,第二次会更快。
  4. State caching. 状态缓存。 The data may be in a more amenable state for subsequent runs. 对于后续运行,数据可能处于更适合的状态。 This can be thought of as sorting an array twice. 这可以被认为是对数组进行两次排序。 The second time is already sorted, which can produce a speed up. 第二次已经排序,这可以加快速度。

Service starting can be a number of seconds. 服务启动可以是几秒钟。

Disk cache approx 20x speed up. 磁盘缓存加速约20倍。 Memory cache approx 6x speed up State caching, can be unbounded. 内存缓存约6倍加速状态缓存,可以无限制。

I think your code needs to reset the scan object, to ensure it does the work again 我认为您的代码需要重置扫描对象,以确保它再次完成工作

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

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