samedi 25 avril 2020

C/C++: How do I compare execution times of two functions (or methods etc.)?

I have two different C/C++ functions that do something. I think one may be a lot faster and I want to find out how much faster. So I do it in a loop a jillion times and time the loop.

Further, I think it might be so fast that the raw time just doing a loop to test it a jillion times could be taking, so I want to subtract out the cost of doing an empty loop.

And yet, code such as the following quite often reports the empty loop taking more time than a loop with a function, despite doing the loop 100 million times. I've taken to doing the empty loop twice but still it's sometimes slower.

Example code comparing two functions with two empty loops:

  tp1 = chrono::high_resolution_clock::now();

  for ( i = 0; i < iRepeats; i++ )
      ;

  tp2 = chrono::high_resolution_clock::now();

  for ( i = 0; i < iRepeats; i++ )
       iLen = Function1();

  tp3 = chrono::high_resolution_clock::now();

  for ( i = 0; i < iRepeats; i++ )
       iLen = Function2();

  tp4 = chrono::high_resolution_clock::now();

  for ( i = 0; i < iRepeats; i++ )
      ;

  tp5 = chrono::high_resolution_clock::now();

  double dDurEmpty1 = chrono::duration<double>( tp2 - tp1 ).count();
  double dDurEmpty2 = chrono::duration<double>( tp5 - tp4 ).count();
  double dDurEmpty  = AKMin( dDurEmpty1, dDurEmpty2 );
  double dDurA = chrono::duration<double>( tp3 - tp2 ).count();
  double dDurB = chrono::duration<double>( tp4 - tp3 ).count();

  printf( "Length:          %7.4fx faster\n",
            ( dDurA - dDurEmpty ) / ( dDurB - dDurEmpty )   );

I'm not wedded to this general approach at all if there's any suggestion of different ways to do it.

Aucun commentaire:

Enregistrer un commentaire