0

I tried calculating function executing time in C++ using methods from this page . Here's my code:

#include <bits/stdc++.h>
#include <sys/time.h>
#include <chrono>
using namespace std;

void myFunc(int a){
  for(int i=0;i<1000000;i++){
    a+=1;
  }
}

int main(){
  int a = 0;

  time_t start1,end1;
  time(&start1);
  ios_base::sync_with_stdio(false);
  myFunc(a);
  time(&end1);
  cout<<fixed<<double(end1-start1)<<setprecision(5)<<endl;

  clock_t start2, end2;
  start2 = clock();
  mtFunc(a);
  end2 = clock();
  cout<<fixed<<double(end2-start2)<<setprecision(5)<<endl;

  struct timeval start3, end3;
  gettimeofday(&start3, NULL);
  ios_base::sync_with_stdio(false);
  myFunc(a);
  Gettimeofday(&end3, NULL);
  cout<<fixed<<double((end3.tv_sec - start3.tv_sec) * 1e6 + (end3.tv_usec - start3.tv_usec)) * 1e-6)<<setprecision(5)<<endl;

  struct timespec start4, end4;
  clock_gettime(CLOCK_MONOTONIC, &start4);
  ios_base::sync_with_stdio(false);
  myFunc(a);
  clock_gettime(CLOCK_MONOTONIC, &end4);
  cout<<fixed<<double((end4.tv_sec - start4.tv_sec) * 1e9 + (end4.tv_nsec - start4.tv_nsec)) * 1e-9)<<setprecision(5)<<endl;

  auto start5 = chrono::high_resolution_clock::now();
  ios_base::sync_with_stdio(false);
  myFunc(a);
  auto end5 = chrono::high_resolution_clock::now();
  cout<<fixed<<chrono::duration_cast<chrono::nanoseconds>(end5 - start5).count()<<setprecision(9)<<endl;
  return 0;
}

All methods prints 0.000000. Tried many times and it didn't changed. I also tried the solution from this answer but still not working. What's wrong here?

EDIT

I've read some comments and find out that myFunc() doesn't do anything so the compiler optimizes it. So I write a new myFunc() as below:

int myFunc(){
  int a = 0;
  for(int i=0;i<1000000;i++){
    a+=1;
  }
  return a;
}

And I changed the calling of function as: int temp = myFunc();.
Suprisingly still nothing worked.

Trung Kiên
  • 135
  • 1
  • 8
  • The measurements are correct.You function `myFunc` doesn't do anything that could have observable side effects, so the compiler is just going to optimize it away completely. – user17732522 Apr 01 '22 at 03:53
  • `myFunc` doesn't do anything. The compiler is free to optimize it away. – NathanOliver Apr 01 '22 at 03:53
  • This code shouldn't even compile because `void myFunc(a)` is not a valid function signature. Assuming it's a typo for something like `void myFunc(int a)`, note that an optimizing compiler following [the as-if rule](https://en.cppreference.com/w/cpp/language/as_if) can and will optimize `myFunc` down to `void myFunc(int a){ return; }` because it has no observable effects. – Nathan Pierson Apr 01 '22 at 03:54
  • @user17732522 I've updated the question, and changed ```myFunc()```, but still nothing works. – Trung Kiên Apr 01 '22 at 04:04
  • @TrungKiên The function calls still has no observable side effect since the return value isn't used. The calls will still be completely optimized away. Even if you e.g. print the result, the whole function is going to be optimized to a single add instruction. – user17732522 Apr 01 '22 at 04:06
  • You also still have several typos in there. Please always copy-paste code that you have actually run. Don't try to rewrite it manually. – user17732522 Apr 01 '22 at 04:08

1 Answers1

0

On my system I get these results

0.000000
2.00000
1537400

once I corrected all the typos in your code

I took out the clock_gettime and gettimeofday ones becuase my system doesnt have them

The point is that your function executes really fast (assuming that its not optimized away entirely)

The first one is calculating the time elapsed in seconds, you function is certainly faster than 1 second so it shows 0

The second one shows how many clock ticks elapsed - look up the man page of clock to see how to convert to real time

The last one is in nano seconds. Which shows you how long it took

I tweaked your code to force the function to do something

int myFunc(int a, int inc) {
    for (int i = 0; i < 1000000; i++) {
        a += inc;
    }
    return a;
}

and

   auto start5 = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(false);
    a = 0;
    a = myFunc(a, 1);
    auto end5 = chrono::high_resolution_clock::now();
    cout << a << " " << fixed << chrono::duration_cast<chrono::nanoseconds>(end5 - start5).count() << setprecision(9) << endl;
  

forcing the function to do work. Just in case it is optimized out

I got

1000000 1177400
pm100
  • 48,078
  • 23
  • 82
  • 145
  • Your numbers look like you compiled without optimizations enabled though. Benchmarking without optimizations enabled is pretty pointless. With your modified function, the compiler should manage to optimize it to a single `imul`/`add` instruction pair and probably will even be constant-folded into the output line, so nothing changed. – user17732522 Apr 01 '22 at 04:13