binarySearchTime.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <ctime>
00003 #include <cstring>
00004 #include <vector>
00005 
00006 #include <TRandom2.h>
00007 #include <TMath.h>
00008 #include <TStopwatch.h>
00009 
00010 #include <TApplication.h>
00011 #include <TCanvas.h>
00012 #include <TH2F.h>
00013 #include <TGraph.h>
00014 #include <TLegend.h>
00015 #include <TAxis.h>
00016 
00017 using namespace std;
00018 
00019 const int npass = 100000;
00020 const int maxint = 100;//20;
00021 const int minsize = 1000;//20;
00022 const int maxsize = 1500;//500;
00023 const int increment = 10;
00024 const int arraysize = (maxsize-minsize)/10 + 1;
00025 
00026 bool showGraphics = true;
00027 
00028 template <typename T> void testBinarySearch(const int n, double* tTMath, double* tStd)
00029 {
00030    vector<T> k(n);
00031    TStopwatch t; 
00032 
00033    TRandom2 r( time( 0 ) );
00034    for ( Int_t i = 0; i < n; i++) {
00035       k[i] = (T) r.Integer( maxint ); 
00036    }
00037 
00038    std::sort(k.begin(), k.end());
00039 
00040    int s = 0; 
00041    t.Start(); 
00042    for (int j = 0; j < npass; ++j) { 
00043       for ( T elem = 0; elem < maxint; ++elem ) {
00044          Long_t index = TMath::BinarySearch((Long_t) n, &k[0], elem);
00045          s += index; 
00046       }
00047    }
00048    t.Stop(); 
00049    *tTMath = t.RealTime();
00050    cout << "TMath::BinarySearch time :\t " << t.RealTime() << endl;
00051    cout << "sum " << s << endl;
00052 
00053    s = 0;
00054    t.Start(); 
00055    for (int j = 0; j < npass; ++j) { 
00056       for ( T elem = 0; elem < maxint; ++elem ) {
00057          T* pind;
00058          pind = std::lower_bound(&k[0], &k[n], elem);
00059          Long_t index2 = ((*pind == elem)? (pind - &k[0]): ( pind - &k[0] - 1));
00060          s+= index2;
00061       }
00062    }
00063    t.Stop(); 
00064    *tStd = t.RealTime();
00065    std::cout << "std::binary_search time:\t " << t.RealTime() << '\n' << std::endl;
00066    cout << "sum " << s << endl;
00067 
00068 }
00069 
00070 void binarySearchTime()
00071 {
00072    vector<double> tM( arraysize );
00073    vector<double> tS( arraysize );
00074    vector<double> index( arraysize );
00075 
00076    //cout << (maxsize-minsize)/10 + 1 << endl;
00077 
00078    for ( int i = minsize; i <= maxsize; i += increment)
00079    {
00080       testBinarySearch<Double_t>(i, &tM[(i-minsize)/10], &tS[(i-minsize)/10]);
00081       index[(i-minsize)/10] = i;
00082    }
00083 
00084    for ( int i = minsize; i <= maxsize; i += increment)
00085       cout << tM[(i-minsize)/10] << ' ' << tS[(i-minsize)/10] << endl;
00086 
00087    if ( showGraphics )
00088    {
00089       TCanvas* c1 = new TCanvas("c1", "Comparision of Searching Time", 600, 400);
00090       TH2F* hpx = new TH2F("hpx", "Comparision of Searching Time", arraysize, minsize, maxsize, arraysize, 0.25,tM[arraysize-1]+0.25);
00091       hpx->SetStats(kFALSE);
00092       hpx->Draw();
00093       
00094       TGraph* gM = new TGraph(arraysize, &index[0], &tM[0]);
00095       gM->SetLineColor(2);
00096       gM->SetLineWidth(3);
00097       gM->SetTitle("TMath::BinarySearch()");
00098       gM->Draw("SAME");
00099       
00100       TGraph* gS = new TGraph(arraysize, &index[0], &tS[0]);
00101       gS->SetLineColor(3);
00102       gS->SetLineWidth(3);
00103       gS->SetTitle("std::binary_search()");
00104       gS->Draw("SAME");
00105       
00106       TLegend* legend = new TLegend(0.15,0.72,0.4,0.86);
00107       legend->AddEntry(gM, "TMath::BinarySearch()");
00108       legend->AddEntry(gS, "std::binary_search()");
00109       legend->Draw();
00110       
00111       hpx->GetXaxis()->SetTitle("Array Size");
00112       hpx->GetYaxis()->SetTitle("Time");
00113       
00114       
00115       c1->Show();
00116    }
00117 
00118    cout << "Test done!" << endl;
00119 }
00120 
00121 int main(int argc, char **argv)
00122 {
00123    if ( argc > 1 && argc != 2 )
00124    {
00125       cerr << "Usage: " << argv[0] << " [-ng]\n";
00126       cerr << "  where:\n";
00127       cerr << "     -ng : no graphics mode";
00128       cerr << endl;
00129       exit(1);
00130    }
00131 
00132    if ( argc == 2 && strcmp( argv[1], "-ng") == 0 ) 
00133    {
00134       showGraphics = false;
00135    }
00136 
00137    TApplication* theApp = 0;
00138    if ( showGraphics )
00139       theApp = new TApplication("App",&argc,argv);
00140 
00141 
00142    binarySearchTime();
00143    cout << argc << endl;
00144 
00145    if ( showGraphics )
00146    {
00147       theApp->Run();
00148       delete theApp;
00149       theApp = 0;
00150    }
00151 
00152    return 0;
00153 }

Generated on Tue Jul 5 14:34:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1