【发布时间】:2013-09-12 20:09:43
【问题描述】:
我正在尝试获取我的程序的经过时间。实际上我认为我应该使用来自time.h 的yclock()。但它在程序的所有阶段都保持为零,尽管我添加了 10^5 个数字(必须消耗一些 CPU 时间)。我已经搜索过这个问题,似乎运行 Linux 的人只有这个问题。我正在运行 Ubuntu 12.04LTS。
我将比较 AVX 和 SSE 指令,因此使用 time_t 并不是一个真正的选择。有什么提示吗?
代码如下:
//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,没有区别。
start_of_programm 0
after_init_of_a 0
after_init_of_b 0
after_add 0
CLOCKS_PER_SEC 1000000
【问题讨论】:
-
“我正在尝试计算我的进程运行的时间”...您的意思是经过的时间?
clock()返回进程使用的 CPU 时间量。 -
真的。我不是天生的演讲者。无论如何,对解决我的问题有什么建议吗?
-
如果在
N上再添加两个0s 会怎样? -
如果您可以访问 C++11(任何最近的编译器),请尝试使用
std::chrono而不是 C 时间函数。 -
@toebs,如果您要比较 AVX 和 SSE 代码,那么您可能希望使用 32 字节对齐的内存以及更大的数组大小。在那种情况下,在堆栈上分配你的数组是行不通的。我会使用
_mm_malloc(sizeof(int)*N, 32)。