c++ - 运行时计时功能和命令行时间之间的区别

标签 c++ timing difference

我有以下代码和来自 https://stackoverflow.com/a/1861493/4587344 的计时功能和来自 https://github.com/IlyaGrebnov/libbsc 的 libbsc .

#include <vector>
#include <iostream>

#include <random>
#include <cassert>

#include <sys/time.h> 

#include "libbsc/libbsc/libbsc.h"

typedef unsigned long long timestamp_t;

static timestamp_t
get_timestamp ()
{
  struct timeval now;
  gettimeofday (&now, NULL);
  return  now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}

int main() {

  int min = 0;
  int max = 1800;
  std::vector<int> rand_vector;

  int  bsc_length;
  int bsc_status;
  double secs_bsc;
  unsigned char* bsc_out_buffer;

  // generate random values
  for (int i=1; i<262144; ++i) rand_vector.push_back(min + (rand() % (int)(max - min + 1))); // 262144 random_integers

  char* rand_buffer = reinterpret_cast<char*> (&rand_vector[0]);
  int rand_length = sizeof (int)*rand_vector.size();

  bsc_out_buffer = (unsigned char*) malloc (rand_length + LIBBSC_HEADER_SIZE);
  const unsigned char* bsc_in_buffer = reinterpret_cast<const unsigned char*> (rand_buffer);

  bsc_status = bsc_init(LIBBSC_FEATURE_FASTMODE);

  if (bsc_status == LIBBSC_NO_ERROR){



    timestamp_t t0_bsc = get_timestamp();
    bsc_length = bsc_compress(bsc_in_buffer, // inbuffer
                  bsc_out_buffer, // outbuffer
                  rand_length, // length of inbuffer
                  LIBBSC_DEFAULT_LZPHASHSIZE,  // int lzpHashSize, 
                  LIBBSC_DEFAULT_LZPMINLEN, //int lzpMinLen, 
                  LIBBSC_DEFAULT_BLOCKSORTER, //int blockSorter, 
                  LIBBSC_CODER_QLFC_ADAPTIVE, //int coder, 
                  LIBBSC_FEATURE_FASTMODE //int features
                );
    timestamp_t t1_bsc = get_timestamp();

    secs_bsc = (t0_bsc - t1_bsc) / 1000000.0L;

  }
  else std::cout << "ERROR in bsc_init: " << bsc_status << std::endl;


  std::cout << std::fixed << "bsc_compress runtime: " << secs_bsc << std::endl;
  std::cout << "bsc_compress size: " << bsc_length << std::endl;

  unsigned char* bsc_assert_buffer;
  bsc_assert_buffer = (unsigned char*) malloc (rand_length);

  bsc_status = bsc_decompress(bsc_out_buffer, bsc_length, bsc_assert_buffer, rand_length, LIBBSC_FEATURE_FASTMODE);
  int* uncompress_values = (int*)bsc_assert_buffer;

  for(int i = 0; i < rand_vector.size(); ++i) {
      assert(uncompress_values[i] == rand_vector[i]);
  }


}

编译它 g++ --std=c++11 test_bsc.cpp libbsc/libbsc.a -o bsc_test 并执行它 时间./bsc_test

输出是

 bsc_compress runtime: 18446744073709.355469
 bsc_compress size: 357178

 real    0m0.392s
 user    0m0.384s
 sys     0m0.008s

谁能给我解释一下区别?计时功能的分辨率不够好? 如果我使用 zlib 压缩,我会得到类似 1.24294 的运行时,具有相同的计时函数和 vector 大小。

关于时间

最佳答案

bsc_compress runtime: 18446744073709.355469

作为一名程序员,您需要记住几个神奇的数字。 2的幂排在最前面,先背15、16、31、32、63、64。后者是一场比赛。这有助于您找到错误,您得到了一个被解释为无符号 64 位值的否定结果。修复:

secs_bsc = (t1_bsc - t0_bsc) / 1000000.0L;

Fwiw:您不能很好地比较这两个结果,操作系统测量包括加载可执行文件和启动 CRT 所需的时间。

关于c++ - 运行时计时功能和命令行时间之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784065/

相关文章:

c++ - tensorflow 将 softmax op 放在 cpu 而不是 gpu 上

c++ - Arduino 中 'StandardCplusplus' 库的问题

javascript - 如何使 `setInterval` 表现得更同步,或者如何使用 `setTimeout` 代替?

python - np.dot 和 np.multiply 与 np.sum 在二进制交叉熵损失计算中的区别

image - CV - 提取两个图像之间的差异

c++ - C++中的简单继承

C++ 继承 : the virtual method of the derived class is not called

python - 在上下文中模拟计时以创建具有字段 DateTimeField 和 auto_now_add=True 的模型

c# - 精确测量线程中代码的执行时间(C#)

java - Java 7 中以月和日为单位的 2 个日期之间的差异