释放模式时序不正确

Incorrect timing in release mode

我正在尝试测量以下代码的执行时间:

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <chrono>

uint64_t LCG(uint64_t LCG_state)
{
    LCG_state = (LCG_state * 2862933555777941757 + 1422359891750319841);
    return LCG_state;
}

int main()
{
auto begin = std::chrono::high_resolution_clock::now();
    uint64_t LCG_state = 333;
    uint32_t w;

    for(int i=0; i<640000000; i++)
    {
        LCG_state = LCG(LCG_state);
        w = LCG_state >> 32;
        //std::cout << w << "\n";
    }

auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
}

我在 Code Blocks 中使用选项发布(因为我认为如果我想正确衡量它,我应该这样做)。问题是每次测量的时间都是 0 秒。更重要的是,如果我要循环:

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <chrono>

uint64_t LCG(uint64_t LCG_state)
{
    LCG_state = (LCG_state * 2862933555777941757 + 1422359891750319841);
    return LCG_state;
}

int main()
{
auto begin = std::chrono::high_resolution_clock::now();
    uint64_t LCG_state = 333;
    uint32_t w;

    for(int i=0; i<10000; i++)
    {
    for(int i=0; i<640000000; i++)
    {
        LCG_state = LCG(LCG_state);
        w = LCG_state >> 32;
        //std::cout << w << "\n";
    }
    }

auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
}

然后仍然测量时间为0秒。在调试 trybe 中一切正常,但是用调试测量代码时间没有意义,对吗?特别是我想将它与此进行比较:

#include <stdint.h>
#include <iostream>

uint64_t s[2] = {5,11};

uint64_t result;

uint64_t next(void) {
  uint64_t s1 = s[0];
  uint64_t s0 = s[1];
  uint64_t result = s0 + s1;
  s[0] = s0;
  s1 ^= s1 << 23; // a
  s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c
  return result;
}

int main()
{
    for(int i=0; i<160000000; i++)
    //while (true)
    {
    //std::cout << next() << "\n";
    result = next();
    //char *c = reinterpret_cast<char*>(&result);
    //std::cout.write(reinterpret_cast<char*>(&result), sizeof result);
    }
}

我想知道什么更快。怎么衡量才合适?为什么执行时间是0秒,难道代码根本不执行?

您可以根据变量 w

添加空 asm 语句
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <chrono>

uint64_t LCG(uint64_t LCG_state)
{
    LCG_state = (LCG_state * 2862933555777941757 + 1422359891750319841);
    return LCG_state;
}

int main()
{
auto begin = std::chrono::high_resolution_clock::now();
    uint64_t LCG_state = 333;
    uint32_t w;

    for(int i=0; i<640000000; i++)
    {
        LCG_state = LCG(LCG_state);
        w = LCG_state >> 32;
        __asm__ volatile("" : "+g" (w) : :);
    }

auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
}

这对编译器来说是不透明的,会阻止循环被优化掉