C ++时钟保持为零 [英] C++ clock stays zero

查看:198
本文介绍了C ++时钟保持为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取我的程序的时间。其实我想我应该使用 yclock() time.h 。但它在程序的所有阶段保持为零,虽然我添加10 ^ 5数字(必须有一些CPU时间消耗)。我已经搜索这个问题,似乎,运行Linux的人都有这个问题。我正在运行Ubuntu 12.04LTS。

Im trying get the elapsed time of my program. Actually i thought I should use yclock() from time.h. But it stays zero in all phases of the program although I'm adding 10^5 numbers(there must be some CPU time consumed). I already searched this problem and it seems like, people running Linux are having this issue only. I'm running Ubuntu 12.04LTS.

我要比较AVX和SSE指令,因此使用 time_t 不是一个选项。任何提示?

I'm going to compare AVX and SSE instructions, so using time_t is not really an option. Any hints?

以下是代码:

 //Dimension of Arrays
unsigned int N = 100000;
//Fill two arrays with random numbers
unsigned  int a[N];
clock_t start_of_programm = clock();
for(int i=0;i<N;i++){
    a[i] = i;
}
clock_t after_init_of_a = clock();
unsigned  int b[N];
for(int i=0;i<N;i++){
    b[i] = i;
}
clock_t after_init_of_b = clock();

//Add the two arrays with Standard
unsigned int out[N];
for(int i = 0; i < N; ++i)
    out[i] = a[i] + b[i];
clock_t after_add = clock();

cout  << "start_of_programm " << start_of_programm  << endl; // prints
cout  << "after_init_of_a " << after_init_of_a  << endl; // prints
cout  << "after_init_of_b " << after_init_of_b  << endl; // prints
cout  << "after_add " << after_add  << endl; // prints
cout  << endl << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << endl;

和控制台的输出。我也用 printf()%d ,没有区别。

And the output of the console. I also used printf() with %d, with no difference.

start_of_programm 0
after_init_of_a 0
after_init_of_b 0
after_add 0

CLOCKS_PER_SEC 1000000


推荐答案

获取时间的最简单的方法是使用OpenMP的存根函数。这将工作在MSVC,GCC和ICC。使用MSVC甚至不需要启用OpenMP。使用ICC,如果你喜欢 -openmp-stubs ,就可以链接存根。使用GCC,您必须使用 -fopenmp

The simplest way to get the time is to just use a stub function from OpenMP. This will work on MSVC, GCC, and ICC. With MSVC you don't even need to enable OpenMP. With ICC you can link just the stubs if you like -openmp-stubs. With GCC you have to use -fopenmp.

#include <omp.h>

double dtime;
dtime = omp_get_wtime();
foo();
dtime = omp_get_wtime() - dtime;
printf("time %f\n", dtime);

这篇关于C ++时钟保持为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆