相关疑难解决方法(0)

如何使用struct timeval来获取执行时间?

在阅读了这篇关于经过时间的文章之后,我写了一个简单的代码来计算循环的执行时间:

#include <stdio.h>
#include <sys/time.h>

int main (int argc, char** argv) {
    struct timeval, tvalBefore, tvalAfter;

    gettimeofday (&tvalBefore, NULL);
    int i =0;
    while ( i < 1000) {
        i ++;
    }

    gettimeofday (&tvalAfter, NULL);

    printf("Time in microseconds: %0.3f microseconds\n",
            (float)(tvalAfter.tv_sec - tvalBefore.tv_sec) 
          )
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

clang编译器给我以下错误:

print_time.c:7:16: error: expected identifier or '('
        struct timeval, *tvalBefore, *tvalAfter;
                      ^
print_time.c:13:17: error: use of undeclared identifier 'tvalBefore'
        gettimeofday (&tvalBefore, NULL);
                       ^
print_time.c:19:17: error: use of undeclared identifier 'tvalAfter' …
Run Code Online (Sandbox Code Playgroud)

c

10
推荐指数
2
解决办法
11万
查看次数

在C中使用多线程进行矩阵乘法

因此,我试图编写一个程序来使用多个线程进行矩阵乘法,然后绘制所用时间和所用线程数之间的图表。我使用了以下方法:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
pthread_mutex_t lock;

#define M 200
#define N 300
#define P 400
#define X 2 // Number of Threads
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"

int A[M][N], B[N][P], C[M][P], D[M][P];

int row = 0;

void *matrixMulti(void *arg)
{
    pthread_mutex_lock(&lock);
    int i = row++;

    for (int j = 0; j < P; j++)
    {
        C[i][j] = 0;
        for (int k = 0; k < N; k++)
        {
            C[i][j] += …
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads matrix matrix-multiplication

2
推荐指数
1
解决办法
799
查看次数

如何以毫秒为单位获取UTC时间

我试图使用gettimeofday函数,但它计算了自大纪元以来所经过的时间,这不是我需要的.

有人可以帮忙吗?

c++ linux utc

1
推荐指数
1
解决办法
3376
查看次数

标签 统计

c ×2

c++ ×1

linux ×1

matrix ×1

matrix-multiplication ×1

multithreading ×1

pthreads ×1

utc ×1