C++ "time.h"测量的运行时间是实际值的两倍

measured runtime from c++ "time.h" is double than real

本文关键字:两倍 time 测量 运行时间 C++      更新时间:2023-10-16

我正在笔记本电脑上运行这个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 秒。