linux - 为什么我的程序存储器写入速度比读取速度快?

标签 linux debian kernel memory

我的简单程序:

//usage:
//indent ./a.c;gcc -O0 ./a.c
//./a.out max r/w repeat timeout

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int
main (int argc, char **argv)
{
  time_t const start_time = time (NULL);
  time_t timeout;
  int max;
  int repeat;
  if (argc == 5)
    {
      max = atoi (argv[1]);
      repeat = atoi (argv[3]);
      timeout = ((time_t) (atoi (argv[4])));
    }
  else
    return 1;

  unsigned char **block_array =
    calloc (sizeof (unsigned char *), (size_t) (max));

  size_t block_length = (size_t) (1024u * 1024u);

  unsigned char data[3];
  data[0] = 'a';
  data[1] = 'b';
  data[2] = 'c';

  unsigned i = 0u;
  //initialize block_array
  for (i = 0u; i < max; i++)
    {
      do
    {
      if ((timeout > ((time_t) (0)))
          && ((time (NULL) - start_time) > timeout))
        {
          puts ("timeouted!");
          return 0;
        }
      block_array[i] = malloc (block_length);
      if (block_array[i] != NULL)
        {
          unsigned bi = 0u;
          for (bi = 0u; bi < block_length; bi++)
        block_array[i][bi] = data[bi % ((unsigned) (sizeof (data)))];
        }
      else
        {
          printf ("%u error\n", i);
        }
    }
      while (NULL == block_array[i]);
    }
  puts ("init ok");

  unsigned score = 0u;
//do page read test
  if ('r' == argv[2][0])
    for (;;)
      {
    for (i = 0u; i < max; i++)
      {
        if ((timeout > ((time_t) (0)))
        && ((time (NULL) - start_time) > timeout))
          {
        puts ("timeouted!");
        goto show_score;
          }

        unsigned bi = 0u;
        for (bi = 0u; bi < block_length; bi++)
          {
        data[bi % ((unsigned) (sizeof (data)))] = block_array[i][bi];
          }
        score++;
      }
    if (repeat >= 0)
      {
        repeat--;
        if (0 == repeat)
          goto show_score;
      }
      }
//do page write test
  else if ('w' == argv[2][0])
    for (;;)
      {
    for (i = 0u; i < max; i++)
      {
        if ((timeout > ((time_t) (0)))
        && ((time (NULL) - start_time) > timeout))
          {
        puts ("timeouted!");
        goto show_score;
          }

        unsigned bi = 0u;
        for (bi = 0u; bi < block_length; bi++)
          {
        block_array[i][bi] = data[bi % ((unsigned) (sizeof (data)))];
          }
        score++;
      }
    if (repeat >= 0)
      {
        repeat--;
        if (0 == repeat)
          goto show_score;
      }
      }
show_score:
  printf ("score:%u\n", score);
  return 0;
}

我同样测试了 Debian Jessie(Linux 3.16)(较少测试)和 Debian Stretch(Linux 4.9)(更多测试肯定)

我已经重复多次相同的测试来确定这一点,所以我只发布一个简短的结果。

测试结果:

$ cat /proc/meminfo |grep SwapTotal
SwapTotal:             0 kB
$ time ./a.out 100 r  5 -1
init ok
score:500

real    0m2.689s
user    0m2.604s
sys 0m0.080s
$ time ./a.out 100 w  5 -1
init ok
score:500

real    0m2.567s
user    0m2.496s
sys 0m0.060s
$ 

最佳答案

在“r”和“w”两种情况下,循环内的主要赋值都是从内存中读取并写回内存,即它们本质上是相同的——您并不是在真正测试内存读取与内存写入。事实证明,每种情况下的时间都非常接近。

'w' 的情况可能会稍微快一些,因为缓存可能包含您想要从内存中读取的值,因为在这种情况下您没有更改源地址。

关于linux - 为什么我的程序存储器写入速度比读取速度快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344563/

相关文章:

c - 我正在尝试在 Ubuntu 12.04.1 中进行系统调用。我在编译 hello.c 文件时遇到错误

regex - 理解 sed 命令中的正则表达式

ruby-on-rails - 启动时在 Raspberry PI 上启动瘦服务器

debian - 从 wsl2 debian 打开文件资源管理器

matlab - 为什么在 matlab 中使用带有 libsvm 的预计算内核

c - sparc_do_fork() 究竟做了什么?

linux - 运行 Anaconda 导航器时出错

python - 如何在 python 中保持子进程运行并持续向其提供输出?

c++ - 带有 MariaDB 的 MySQL 连接器/C++

exception - 页面错误、页面短缺或访问冲突?