C++ "time.h"测量的运行时间是实际值的两倍
measured runtime from c++ "time.h" is double than real
我正在笔记本电脑上运行这个pthread-c ++程序(高斯消除)来测量它的运行时。该程序实际运行大约 10 秒,但我的输出显示大约 20 秒。这个程序有什么问题?
我用了
g++ -pthread main.c
./a.out 32 2048
运行
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <pthread.h>
#include <iostream>
typedef float Type;
void mat_rand (Type**, int, int);
Type** mat_aloc (int, int);
void mat_free (Type**);
void mat_print (Type**, int, int);
void* eliminate(void*);
unsigned int n, max_threads, active_threads, thread_length;
Type** A;
int current_row;
struct args
{
int start;
int end;
};
typedef struct args argument;
void *print_message_function( void *ptr );
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf ("Error!. Please Enter The Matrix Dimension and No. of Threads!n");
return 0;
} else
{
n = atoi(argv[2]);
max_threads = atoi(argv[1]);
if (n > 4096)
{
printf ("The maximum allowed size is 4096!n");
return 0;
}
if (max_threads > 32)
{
printf ("The maximum allowed Threads Count is 32!n");
return 0;
}
}
A = mat_aloc(n , n+1);
mat_rand (A, n, n+1);
//mat_print (A, n, n+1);
std::clock_t start;
double exe_time;
start = std::clock();
pthread_attr_t attr;
pthread_attr_init(&attr);
argument* thread_args = new argument[max_threads];
pthread_t* thread = new pthread_t[max_threads];
for (int i=0; i<n-1; i++)
{
current_row = i;
if (max_threads >= n-i)
active_threads = n-i-1;
else
active_threads = max_threads;
thread_length = (n-i-1)/active_threads;
for (int j=0; j<active_threads-1; j++)
{
thread_args[j].start = i+1+j*thread_length;
thread_args[j].end = i+1+(j+1)*thread_length;
pthread_create( &thread[j], &attr, eliminate, (void*) &thread_args[j]);
}
thread_args[active_threads-1].start = i+1+(active_threads-1)*thread_length;
thread_args[active_threads-1].end = n-1;
pthread_create(&thread[active_threads-1], &attr, eliminate, (void*) &thread_args[active_threads-1]);
for (int j=0; j<active_threads; j++)
{
pthread_join(thread[j], NULL);
}
}
exe_time = (clock() - start) / (double) CLOCKS_PER_SEC;
printf("Execution time for Matrix of size %i: %fn", n, exe_time);
//mat_print (A, n, n+1);
return 0;
}
void* eliminate(void* arg)
{
Type k, row_constant;
argument* info = (argument*) arg;
row_constant = A[current_row][current_row];
for (int i=info->start; i<=info->end; i++)
{
k = A[i][current_row] / row_constant;
A[i][current_row] = 0;
for (int j=current_row+1; j<n+1; j++)
{
A[i][j] -= k*A[current_row][j];
}
}
}
// matrix random values
void mat_rand (Type** matrix, int row, int column)
{
for (int i=0; i<row; i++)
for (int j=0; j<column; j++)
{
matrix[i][j] = (float)(1) + ((float)rand()/(float)RAND_MAX)*256;
}
}
// allocates a 2d matrix
Type** mat_aloc (int row, int column)
{
Type* temp = new Type [row*column];
if (temp == NULL)
{
delete [] temp;
return 0;
}
Type** mat = new Type* [row];
if (temp == NULL)
{
delete [] mat;
return 0;
}
for (int i=0; i<row; i++)
{
mat[i] = temp + i*column;
}
return mat;
}
// free memory of matrix
void mat_free (Type** matrix)
{
delete[] (*matrix);
delete[] matrix;
}
// print matrix
void mat_print (Type** matrix, int row, int column)
{
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
std::cout<< matrix[i][j] << "tt";
}
printf("n");
}
printf(".................n");
}
>clock
报告使用的CPU时间。如果您有 2 个 CPU 并在每个 CPU 上运行一个线程 10 秒,clock
将报告 20 秒。
相关文章:
- 重复使用预分配的向量<复杂<double>>作为<double>长度两倍的向量
- 计算C++两倍的差值
- 为什么当我进入退出条件时,程序会打印两倍的默认条件而不是退出 while 循环?
- 内存移动的性能与memcpy相比是两倍?
- Qt 是 JSON 精度的两倍
- 不完整的多线程光线追踪器花费的时间是预期的两倍
- C++随机函数给出的相同整数是输出的两倍
- 为什么我的最终向量是它应该大小的两倍并且具有前导 0?
- 为什么即使在从int到两倍的类型施放后,小数也没有小数
- 如何将固定精度的两倍格式化为给定的长度
- getsockopt() 返回的值是之前由 setsockopt() 设置的值的两倍
- MF SinkWriter mp4文件的播放持续时间是添加音频样本时的一半,图像的播放速度也是添加音频样本的两倍
- 无论如何,是否可以使用setCursorPos(int,int)函数,但没有采用两个int使它需要速度较慢的两倍
- 是一个包含两个INT(只有两个INT)的结构,保证是大小(INT)的两倍
- C++ "time.h"测量的运行时间是实际值的两倍
- 退出申请致电两倍
- SSE42 & STTNI - PcmpEstrM比PcmpIstrM慢两倍,是真的吗?
- 在同一台机器上,是否保证两倍值的不精确性是一致的
- 处理数据的方法,速度是我处理速度的两倍
- C++11 getline 要求输入的两倍