c - 尝试运行一个程序,在运行函数 copyij() 和 copyji() 后以秒为单位跟踪耗时

标签 c

我在找什么:
copyij():dim=2048:经过=0.13 秒
copyji():dim=2048:经过=0.97 秒

我已经尝试过的:

使用代表延迟秒数的 del_sec 变量并将其指定为延迟毫秒数/NNN(这是另一个人的建议,我不知道他为什么建议我将毫秒除以 NNN,但它使我更接近。

它的 copy ij() 和 copy ji() 都经过了 0.3485 秒,这很接近,但您知道他们对雪茄的看法。

我认为的问题是:

我知道这与 del_sec 变量有关,因为骨架程序(我正在使用的那个)中的 printf 函数有效地使得经过的数字将打印一个带有 3 个小数位的无符号数字(但是,这不会发生.)

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

#define NNN 2048

void copyij();
void copyji();
void init_mat();
int64_t time_diff();

int src[NNN][NNN], dst[NNN][NNN];

int main(int argc,char **argv) {

  int i,j,n;

  long int del_sec,del_msec;
  struct timeval tv_s,tv_e;

  init_mat();
  gettimeofday(&tv_s, NULL); 
  copyij();
  gettimeofday(&tv_e, NULL); 

  del_sec = del_sec/del_msec/NNN;
  /* fill here to compute elapsed time */

  printf("copyij(): dim=%d: elapsed=%ld.%03ld secs\n", NNN , del_sec , del_msec/1000);


  init_mat();
  gettimeofday(&tv_s, NULL); 
  copyji();
  gettimeofday(&tv_e, NULL); 

     del_sec = del_sec/del_msec/NNN;
  /* fill here to compute elapsed time */

  printf("copyji(): dim=%d: elapsed=%ld.%03ld secs\n", NNN , del_sec, del_msec/1000 );

  return 0;
}

void copyij(){
  int i,j;

      for(i = 0; i <NNN; i++)
         for(j=0; j < NNN; j++) 
            src[i][j] =+ 1; 
  /* fill here */

}

void copyji(){
  int i,j;
   for(i = 0; i < NNN; i++)   
      for(j = 0; j < NNN; j++)   
         dst[j][i] += 1; 

  /* fill here */

}

void init_mat(){
  int i,j;

  for (i=0;i<NNN;i++)
    for (j=0;j<NNN;j++) src[i][j] = dst[i][j] = 1;

}

最佳答案

copyij你有

src[i][j] =+ 1;
但我想你的意思是
src[i][j] += 1;
测量时间
为了跟踪耗时,我推荐 clock_gettime 带有单调时钟 CLOCK_MONOTONIC , 或 CLOCK_MONOTONIC_COARSE如果你的系统有。
要查找时间增量,您可以使用以下宏(来自 OpenBSD 的 sys/time.h ,请参阅手册 timespecsub(3) ):
#define timespecsub(tsp, usp, vsp)                        \
    do                                                    \
    {                                                     \
        (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;    \
        (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
        if ((vsp)->tv_nsec < 0)                           \
        {                                                 \
            (vsp)->tv_sec--;                              \
            (vsp)->tv_nsec += 1000000000L;                \
        }                                                 \
    } while (0)
例如:
struct timespec start, end, delta;
timespecsub(&end, &start, &delta);
// delta contains the time difference from start to end
如果您只想对复制功能进行基准测试,则除以 NNN除非您想对每个维度的性能进行基准测试(ij 为行,ji 为列),否则没有意义。
打印 struct timespec您可以使用 %lldtv_sec%.9ldtv_nsec .
编译器优化
为确保编译器不会优化掉您的函数:
  • 使用 volatile对于矩阵。这可以防止编译器优化对它们的操作。
  • static volatile int src[NNN][NNN], dst[NNN][NNN];
    
  • 对于 GCC,使用编译器指令,例如 #pragma GCC optimize("O0")围绕复制功能,以防止 GCC 优化循环。 See here对于替代方案。
  • #pragma GCC push_options
    #pragma GCC optimize("O0")
    
    static void
    copyij()
    {
        // ...
    }
    
    static void
    copyji()
    {
        // ...
    }
    
    #pragma GCC pop_options
    
    如果没有这些预防措施,通过测试 GCC (v11.2.1 20210728) 似乎优化了 -O3 处的功能,但不是 -O2或以下。
    结果
    更新代码 ( gist )
    #include <assert.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NNN 2048
    #define BENCH_COUNT 10
    #define timespecsub(tsp, usp, vsp)                        \
        do                                                    \
        {                                                     \
            (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;    \
            (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
            if ((vsp)->tv_nsec < 0)                           \
            {                                                 \
                (vsp)->tv_sec--;                              \
                (vsp)->tv_nsec += 1000000000L;                \
            }                                                 \
        } while (0)
    
    static void benchmark(void (*f)(void), const char *name);
    static void copyij();
    static void copyji();
    static void init_mat();
    
    static volatile int src[NNN][NNN], dst[NNN][NNN];
    
    int main(void)
    {
        size_t i;
        printf("ij:\n");
        for (i = 0; i < BENCH_COUNT; i++)
        {
            benchmark(copyij, "copyij");
        }
        printf("ji:\n");
        for (i = 0; i < BENCH_COUNT; i++)
        {
            benchmark(copyji, "copyji");
        }
    }
    
    static void
    benchmark(void (*f)(void), const char *name)
    {
        struct timespec start, end, delta;
    
        init_mat();
        assert(clock_gettime(CLOCK_MONOTONIC, &start) != -1);
        f();
        assert(clock_gettime(CLOCK_MONOTONIC, &end) != -1);
    
        timespecsub(&end, &start, &delta);
        printf("%s: NNN=%d: elapsed=%lld.%.9ld secs\n", name, NNN, delta.tv_sec,
               delta.tv_nsec);
    }
    
    #pragma GCC push_options
    #pragma GCC optimize("O0")
    
    static void
    copyij()
    {
        size_t i, j;
    
        for (i = 0; i < NNN; i++)
        {
            for (j = 0; j < NNN; j++)
            {
                src[i][j] += 1;
            }
        }
    }
    
    static void
    copyji()
    {
        size_t i, j;
    
        for (i = 0; i < NNN; i++)
        {
            for (j = 0; j < NNN; j++)
            {
                dst[j][i] += 1;
            }
        }
    }
    
    #pragma GCC pop_options
    
    static void
    init_mat()
    {
        size_t i, j;
    
        for (i = 0; i < NNN; i++)
        {
            for (j = 0; j < NNN; j++)
            {
                src[i][j] = dst[i][j] = 1;
            }
        }
    }
    
    输出(i7-7500U @ 2.7 GHz,WSL 内部,-O3)
    ij:
    copyij: NNN=2048: elapsed=0.020582100 secs
    copyij: NNN=2048: elapsed=0.016620800 secs
    copyij: NNN=2048: elapsed=0.016156000 secs
    copyij: NNN=2048: elapsed=0.017765700 secs
    copyij: NNN=2048: elapsed=0.016158500 secs
    copyij: NNN=2048: elapsed=0.016127900 secs
    copyij: NNN=2048: elapsed=0.016153200 secs
    copyij: NNN=2048: elapsed=0.016337300 secs
    copyij: NNN=2048: elapsed=0.016625900 secs
    copyij: NNN=2048: elapsed=0.016512300 secs
    ji:
    copyji: NNN=2048: elapsed=0.055380300 secs
    copyji: NNN=2048: elapsed=0.056751900 secs
    copyji: NNN=2048: elapsed=0.055770200 secs
    copyji: NNN=2048: elapsed=0.056378700 secs
    copyji: NNN=2048: elapsed=0.057477700 secs
    copyji: NNN=2048: elapsed=0.058508900 secs
    copyji: NNN=2048: elapsed=0.058080200 secs
    copyji: NNN=2048: elapsed=0.057968100 secs
    copyji: NNN=2048: elapsed=0.058937900 secs
    copyji: NNN=2048: elapsed=0.056836300 secs
    
    我怀疑 ji 速度较慢,因为它在非连续内存上运行。

    关于c - 尝试运行一个程序,在运行函数 copyij() 和 copyji() 后以秒为单位跟踪耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357616/

    相关文章:

    c - matlab中c函数返回值错误

    c - 有没有办法将 sscanf 与 stdin 一起使用?

    c - 在 MPI 中实现 Cannons 算法

    c - 如何读取 12 位的 i2c lis3dh 寄存器值?

    c - 现在用 C 管道 IPC

    c - scanf 和空白字符

    c - HSV 转换与 cvCvtColor() 问题

    c - 弄清楚三元运算符级联的功能?

    c - SO_SNDBUF 小时阻塞 0.04 秒

    C - 访问结构数组