ROOT logo

From $ROOTSYS/tutorials/tree/bill.C

// benchmark comparing write/read to/from keys or trees
// for example for N=10000, the following output is produced
// on a P IV 62.4GHz
//   
// root -b -q bill.C    or root -b -q bill.C++
//
//billw0  : RT=  1.070 s, Cpu=  1.050 s, File size=  45508003 bytes, CX= 1
//billr0  : RT=  0.740 s, Cpu=  0.730 s
//billtw0 : RT=  0.720 s, Cpu=  0.660 s, File size=  45163959 bytes, CX= 1
//billtr0 : RT=  0.420 s, Cpu=  0.420 s
//billw1  : RT=  6.600 s, Cpu=  6.370 s, File size=  16215349 bytes, CX= 2.80687
//billr1  : RT=  2.290 s, Cpu=  2.270 s
//billtw1 : RT=  3.260 s, Cpu=  3.230 s, File size=   6880273 bytes, CX= 6.5642
//billtr1 : RT=  0.990 s, Cpu=  0.980 s
//billtot : RT= 18.290 s, Cpu= 15.920 s
//******************************************************************
//*  ROOTMARKS = 600.9   *  Root3.05/02   20030201/1840
//******************************************************************
//
//Author: Rene Brun

#include "TFile.h"
#include "TSystem.h"
#include "TH1.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include "TKey.h"
#include "TTree.h"
#include "TROOT.h"
   
const Int_t N = 10000;       //number of events to be processed
TStopwatch timer;

void billw(Int_t compress) {
   //write N histograms as keys
   timer.Start();
   TFile f("/tmp/bill.root","recreate","bill benchmark with keys",compress);
   TH1F h("h","h",1000,-3,3);
   h.FillRandom("gaus",50000);
   
   for (Int_t i=0;i<N;i++) {
      char name[20];
      sprintf(name,"h%d",i);
      h.SetName(name);
      h.Fill(2*gRandom->Rndm());
      h.Write();
   }
   timer.Stop();
   printf("billw%d  : RT=%7.3f s, Cpu=%7.3f s, File size= %9d bytes, CX= %g\n",compress,timer.RealTime(),timer.CpuTime(),
                    (Int_t)f.GetBytesWritten(),f.GetCompressionFactor());
   }
void billr(Int_t compress) {
   //read N histograms from keys
   timer.Start();
   TFile f("/tmp/bill.root");
   TIter next(f.GetListOfKeys());
   TH1F *h;
   TH1::AddDirectory(kFALSE);
   TKey *key;
   Int_t i=0;
   TH1F *hmean = new TH1F("hmean","hist mean from keys",100,0,1);
   
   while ((key=(TKey*)next())) {
      h = (TH1F*)key->ReadObj();
      hmean->Fill(h->GetMean());
      delete h;
      i++;
   }
   timer.Stop();
   printf("billr%d  : RT=%7.3f s, Cpu=%7.3f s\n",compress,timer.RealTime(),timer.CpuTime());
}
void billtw(Int_t compress) {
   //write N histograms to a Tree
   timer.Start();
   TFile f("/tmp/billt.root","recreate","bill benchmark with trees",compress);
   TH1F *h = new TH1F("h","h",1000,-3,3);
   h->FillRandom("gaus",50000);
   TTree *T = new TTree("T","test bill");
   T->Branch("event","TH1F",&h,64000,0);
   for (Int_t i=0;i<N;i++) {
      char name[20];
      sprintf(name,"h%d",i);
      h->SetName(name);
      h->Fill(2*gRandom->Rndm());
      T->Fill();
   }
   T->Write();
   delete T;
   timer.Stop();
   printf("billtw%d : RT=%7.3f s, Cpu=%7.3f s, File size= %9d bytes, CX= %g\n",compress,timer.RealTime(),timer.CpuTime(),
                    (Int_t)f.GetBytesWritten(),f.GetCompressionFactor());
}
void billtr(Int_t compress) {
   //read N histograms from a tree
   timer.Start();
   TFile f("/tmp/billt.root");
   TH1F *h = 0;
   TTree *T = (TTree*)f.Get("T");
   T->SetBranchAddress("event",&h);
   TH1F *hmeant = new TH1F("hmeant","hist mean from tree",100,0,1);
   Long64_t nentries = T->GetEntries();
   for (Long64_t i=0;i<nentries;i++) {
      T->GetEntry(i);
      hmeant->Fill(h->GetMean());
   }
   timer.Stop();
   printf("billtr%d : RT=%7.3f s, Cpu=%7.3f s\n",compress,timer.RealTime(),timer.CpuTime());
}
void bill() {
   
   TStopwatch totaltimer;
   totaltimer.Start();
   for (Int_t compress=0;compress<2;compress++) {
      billw(compress);
      billr(compress);
      billtw(compress);
      billtr(compress);
   }
   gSystem->Unlink("/tmp/bill.root");
   gSystem->Unlink("/tmp/billt.root");
   totaltimer.Stop();
   Double_t rtime = totaltimer.RealTime();
   Double_t ctime = totaltimer.CpuTime();
   printf("billtot : RT=%7.3f s, Cpu=%7.3f s\n",rtime,ctime);
   //reference is a P IV 2.4 GHz
   Float_t rootmarks = 600*(16.98 + 14.40)/(rtime + ctime);
   printf("******************************************************************\n");
   printf("*  ROOTMARKS =%6.1f   *  Root%-8s  %d/%d\n",rootmarks,gROOT->GetVersion(),gROOT->GetVersionDate(),gROOT->GetVersionTime());
   printf("******************************************************************\n");
}
          
 bill.C:1
 bill.C:2
 bill.C:3
 bill.C:4
 bill.C:5
 bill.C:6
 bill.C:7
 bill.C:8
 bill.C:9
 bill.C:10
 bill.C:11
 bill.C:12
 bill.C:13
 bill.C:14
 bill.C:15
 bill.C:16
 bill.C:17
 bill.C:18
 bill.C:19
 bill.C:20
 bill.C:21
 bill.C:22
 bill.C:23
 bill.C:24
 bill.C:25
 bill.C:26
 bill.C:27
 bill.C:28
 bill.C:29
 bill.C:30
 bill.C:31
 bill.C:32
 bill.C:33
 bill.C:34
 bill.C:35
 bill.C:36
 bill.C:37
 bill.C:38
 bill.C:39
 bill.C:40
 bill.C:41
 bill.C:42
 bill.C:43
 bill.C:44
 bill.C:45
 bill.C:46
 bill.C:47
 bill.C:48
 bill.C:49
 bill.C:50
 bill.C:51
 bill.C:52
 bill.C:53
 bill.C:54
 bill.C:55
 bill.C:56
 bill.C:57
 bill.C:58
 bill.C:59
 bill.C:60
 bill.C:61
 bill.C:62
 bill.C:63
 bill.C:64
 bill.C:65
 bill.C:66
 bill.C:67
 bill.C:68
 bill.C:69
 bill.C:70
 bill.C:71
 bill.C:72
 bill.C:73
 bill.C:74
 bill.C:75
 bill.C:76
 bill.C:77
 bill.C:78
 bill.C:79
 bill.C:80
 bill.C:81
 bill.C:82
 bill.C:83
 bill.C:84
 bill.C:85
 bill.C:86
 bill.C:87
 bill.C:88
 bill.C:89
 bill.C:90
 bill.C:91
 bill.C:92
 bill.C:93
 bill.C:94
 bill.C:95
 bill.C:96
 bill.C:97
 bill.C:98
 bill.C:99
 bill.C:100
 bill.C:101
 bill.C:102
 bill.C:103
 bill.C:104
 bill.C:105
 bill.C:106
 bill.C:107
 bill.C:108
 bill.C:109
 bill.C:110
 bill.C:111
 bill.C:112
 bill.C:113
 bill.C:114
 bill.C:115
 bill.C:116
 bill.C:117
 bill.C:118
 bill.C:119
 bill.C:120
 bill.C:121
 bill.C:122
 bill.C:123
 bill.C:124
 bill.C:125
 bill.C:126
 bill.C:127
 bill.C:128
 bill.C:129
 bill.C:130
 bill.C:131
 bill.C:132