<commit_before>#include <iostream>
#include <algorithm>

#include <TRandom2.h>
#include <TStopwatch.h>
#include <TMath.h>

using std::cout;
using std::endl;

const unsigned int NUMTEST = 500;

// #define DEBUG

template <typename T> T randD() {
   // use default seed to get same sequence
   static TRandom2 r;  
   return (T) r.Uniform(-500,500);
}

double Time(TStopwatch & w) { 
   //return w.CpuTime();  
   return w.RealTime();   
}


template <typename T> double stressVector(unsigned int size, const char* type)
{
   std::cout << "Generating random vector of '" 
        << type << "' and size " << size << " ..." << std::endl << std::endl;

   double totalTime = 0;
   double totalUnitTime = 0;

   T *vector = new T[size];
   std::generate(vector, &vector[size], randD<T>);

#ifdef DEBUG
   for ( unsigned int i = 0; i < size; ++i )
      std::cout << vector[i] << " " << std::endl;
#endif

   TStopwatch w;
   std::cout.precision(6);

   unsigned int ntest = 3 * NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::MinElement(size, vector);
   w.Stop();
   std::cout << "MinMaxElement() \tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = 3 * NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::LocMin(size, vector);
   w.Stop();
   std::cout << "LocMin/Max() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = 10 * NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::Mean(size, vector);
   w.Stop();
   std::cout << "Mean() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( NUMTEST/2.5 );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::Median(size, vector);
   w.Stop();
   std::cout << "Median() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( 10 * NUMTEST );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::RMS(size, vector);
   w.Stop();
   std::cout << "RMS() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( NUMTEST/2.5 );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::GeomMean(size, vector);
   w.Stop();
   std::cout << "GeomMean() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   UInt_t * index =new UInt_t[size];
   ntest = NUMTEST/10;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::Sort(size, vector, index, kFALSE);
   w.Stop();
   std::cout << "Sort() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   std::sort(vector, vector + size);
#ifdef DEBUG
   for ( unsigned int i = 0; i < size; ++i )
      std::cout << vector[i] << " " << std::endl;
#endif
   ntest = 20000*NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::BinarySearch(size, vector, vector[ i % size ]);
   w.Stop();<commit_msg>Improve CPU time measurement<commit_after>#include <iostream>
#include <algorithm>

#include <TRandom2.h>
#include <TStopwatch.h>
#include <TMath.h>

using std::cout;
using std::endl;

const unsigned int NUMTEST = 500;

// #define DEBUG

template <typename T> T randD() {
   // use default seed to get same sequence
   static TRandom2 r;  
   return (T) r.Uniform(-500,500);
}

double Time(TStopwatch & w) { 
   //return w.CpuTime();  
   return w.RealTime();   
}


template <typename T> double stressVector(unsigned int size, const char* type)
{
   std::cout << "Generating random vector of '" 
        << type << "' and size " << size << " ..." << std::endl << std::endl;

   double totalTime = 0;
   double totalUnitTime = 0;

   T *vector = new T[size];
   std::generate(vector, &vector[size], randD<T>);

#ifdef DEBUG
   for ( unsigned int i = 0; i < size; ++i )
      std::cout << vector[i] << " " << std::endl;
#endif

   TStopwatch w;
   std::cout.precision(6);

   unsigned int ntest = 3 * NUMTEST;
   w.Start( kTRUE );
   volatile int iresult = 0; 
   for ( unsigned int i = 0; i < ntest; ++i )
      iresult = TMath::MinElement(size, vector);
   w.Stop();
   std::cout << "MinMaxElement() \tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = 3 * NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      iresult  = TMath::LocMin(size, vector);
   w.Stop();
   std::cout << "LocMin/Max() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = 10 * NUMTEST;
   w.Start( kTRUE );
   volatile double result; 
   for ( unsigned int i = 0; i < ntest; ++i )
      result = TMath::Mean(size, vector);
   w.Stop();
   std::cout << "Mean() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( NUMTEST/2.5 );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      result = TMath::Median(size, vector);
   w.Stop();
   std::cout << "Median() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( 10 * NUMTEST );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      result = TMath::RMS(size, vector);
   w.Stop();
   std::cout << "RMS() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   ntest = (unsigned int) ( NUMTEST/2.5 );
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      result = TMath::GeomMean(size, vector);
   w.Stop();
   std::cout << "GeomMean() \t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   UInt_t * index =new UInt_t[size];
   ntest = NUMTEST/10;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      TMath::Sort(size, vector, index, kFALSE);
   w.Stop();
   std::cout << "Sort() \t\t\tTotal Time: " << Time(w) << "  (s)\t\t" 
        << " Time/call: " << Time(w)/(ntest)*1.E6 << "   (microsec)" << std::endl;
   totalUnitTime += Time(w)/ntest;
   totalTime += Time(w);

   std::sort(vector, vector + size);
#ifdef DEBUG
   for ( unsigned int i = 0; i < size; ++i )
      std::cout << vector[i] << " " << std::endl;
#endif
   ntest = 20000*NUMTEST;
   w.Start( kTRUE );
   for ( unsigned int i = 0; i < ntest; ++i )
      iresult = TMath::BinarySearch(size, vector, vector[ i % size ]);
   w.Stop();
{'fraction_non_alphanumeric': 0.21264222503160557, 'fraction_numerical': 0.014032869785082175, 'mean_word_length': 2.7439659252247988, 'pattern_counts': {'":': 0, '<': 287, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4104004', 'n_tokens_mistral': 3182, 'n_tokens_neox': 3017, 'n_words': 722}