0

What is the reason for two different results of this program? First, the result is in the sandbox of avast antivirus and second without it.

#include <iostream>
#include <chrono>
#include <cmath>
using namespace std;
int main()
{
  chrono::high_resolution_clock::time_point tp_current,tp_previous;
  tp_current=chrono::high_resolution_clock::now();
  int t[16];
  for(int i=0;i<16;++i)
  {
    tp_previous=tp_current;
    tp_current=chrono::high_resolution_clock::now();
    t[i]=(tp_current-tp_previous).count();
    for(int j=0;j<20000;++j)
      sin(j);
  }
  for(int i=0;i<16;++i)
    cout<<t[i]<<endl;
  return 0;
}

1001000
9005000
6004000
7008000
7002000
6005000
7006000
6004000
7013000
5996000
8006000
7005000
6022000
7993000
6019000
6002000

0
0
15625000
0
0
0
0
0
15624000
0
0
0
0
0
0
15625000

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
  • I think it is something wrong with the sandbox environment. chrono fetches the time from the OS which is apparently providing incorrect values. I have seen some issues with time synchronization when you use virtual machine, So theoretically chrono is essentially displaying you what OS gives it. – PRIME Mar 29 '18 at 11:38
  • Maybe the clock resolution is very low on your OS and seldom updated. So if multiple iterations in your loop run in one "tick" you see a 0 and the next step will get the sum of the runs before. On my OS (linux) I get values around 75 to 78 without any down or up bursts. There are other reports on bad results on windows: https://stackoverflow.com/questions/16299029/resolution-of-stdchronohigh-resolution-clock-doesnt-correspond-to-measureme – Klaus Mar 29 '18 at 11:41
  • It's not avast problem. I see now this two resoults alternately without sandbox. – Jan Nowak Mar 29 '18 at 13:17
  • Try inspecting the assembly and changing the optimization level. – Howard Hinnant Mar 29 '18 at 14:02
  • I don't use any optimization. I use mingw32 in Windows 10, Intel Core i3. Maybe threads or cores? – Jan Nowak Mar 29 '18 at 14:58
  • And I use one exe file without recompilation. So it's probably system shares various ticks? And its safe to develop program with that time management? – Jan Nowak Mar 29 '18 at 15:16

0 Answers0