Re: dynamical versus non dynamical for TH1F

From: John Idarraga <idarraga_at_cern.ch>
Date: Sat, 23 Jan 2010 23:10:21 -0500


Hello Marc,

Using 'new' operators to instantiate your objects shouldn't be that different in CPU time (except if you instantiate 10^8 objects like in your second code). Note that when your objects are not created with 'new' then the destructors are called by default and it might make a difference ... still I am puzzled. Are you compiling that code (the first one) ? Can you send the code you are using ?

In you second code the reason you can not use delete is that you are trying to call it off-scope (second loop). Note that you don't delete 'person', you delete 'marc' which is the object. Try this

for (int j=0;j<1000;j++) {

     for (int i=0;i<100000;i++) {
       person *marc=new person();
       marc->age=33;
       delete marc;
     }

}

The second code is indeed faster .... big difference between allocating in the heap and in the stack since again, you are instantiating 10^8 objects. In the case of a few histograms it won't be the case, and you probably need those objects to remain in memory.

for (int j=0;j<1000;j++) {

     for (int i=0;i<100000;i++) {
       person marc;
       marc.age=33;
     }

}

cheers,

John

On Sat, 2010-01-23 at 22:40 +0100, Marc Escalier wrote:
> thanks Andriy for the information
>
> i still put the whole list because somebody could have important
> comments.
>
> i would have some other questions to understand better, if you don't
> mind :
>
> A) functionnality :
> > is that, according to C++ rules, the object myhisto will be destroyed after
> > exiting of scope, e.g. when your macro will terminate (if you draw this
> > histogram in macro, the plot will disappear from TCanvas).
>
> suppose i have a program with something like
> ...
> if (withToDoPlot1) {
> TCanvas *mycanvas=new TCanvas(...)
> TH1F *myhisto1=new TH1F(...)
> "fill of myhisto1"
> "draw of myhisto1"
> }
>
> and an another version, the same, but without pointer
> i mean
> if (...) {
> TCanvas mycanvas(...)
> TH1F myhisto(...)
> ...
> ...draw()
> }
>
> ==>what will be the difference ?
>
> B) performances :
> i just made a dummy test of using or not using dynamical allocation,
> the performance difference is huge : the dynamical is much more slower
>
> example :
> (in addition, in this example, i didn't do the "delete", which is
> "forbidden", just to save a bit more time : even with that, dynamical
> allocation is still very slow)
>
> example :
>
> 1)the dynamic way :
> 0.309 second to run on a given computer
> (i check that the differences of processing time is relevant with
> regards to fluctuations)
>
> #include <iostream>
>
> int main()
> {
> class person
> {
> public :
> int age;
> };
>
> for (int j=0;j<1000;j++) {
> for (int i=0;i<100000;i++) {
> class person *marc=new person();
> marc->age=33;
>
> // std::cout << "age de marc=" << marc->age << std::endl;
> }
>
> // delete person; (notice i don't delete to save time, but i don't
> have the "right"
> }
> }
>
> 2)the static way (actually, i'm not sure that "static" is the good word
> :
> on the same computer :
> 6.3 second : much more slower !!!
>
> #include <iostream>
>
> int main()
> {
> class person
> {
> public :
> int age;
> };
>
> for (int j=0;j<1000;j++) {
> for (int i=0;i<100000;i++) {
> class person *marc=new person();
> marc->age=33;
>
> // std::cout << "age de marc=" << marc->age << std::endl;
> }
>
> // delete person;
> }
> }
>
>
> regards
>
>
> ===============
>
>
> On Sat, 23 Jan 2010, Andriy Zatserklyaniy wrote:
>
> > Hi Mark,
> >
> > In short: a recommended way is to create you histogram in heap
> >
> > TH1F *myhisto=new TH1F(...)
> >
> > Otherwise you have to specify what do you want to optimise: speed, memory
> > usage, etc.
> >
> > Well known drawback of
> >
> > TH1F myhisto(...)
> >
> > is that, according to C++ rules, the object myhisto will be destroyed after
> > exiting of scope, e.g. when your macro will terminate (if you draw this
> > histogram in macro, the plot will disappear from TCanvas).
> >
> > Cheers,
> > Andrei.
> >
> > On Sat, Jan 23, 2010 at 10:48 AM, Marc Escalier <escalier_at_lal.in2p3.fr>wrote:
> >
> >> Hello,
> >>
> >> i'm using a basic root program (.C) that allows to make some histograms of
> >> variables of a given ntuple (a .root file)
> >>
> >> Up to now, i created the histogram dynamically with
> >> TH1F *myhisto=new TH1F(...)
> >>
> >> i was wondering what it the more optimal (in terms of memory, cpu usage, or
> >> any other criterious you know better than me) way to do ? :
> >>
> >> *this kind of dynamical allocation
> >>
> >> or
> >>
> >> *a non dynamical allocation
> >> (i mean :
> >> TH1F myhisto(...)
> >> )
> >>
> >> (just in case : i do always the same plots and there variables are always
> >> in the ntuple)
> >>
> >> thanks !
> >>
> >> (i'll reply in private to answers to prevent spamming you all)
> >>
> >>
> >
>
Received on Sun Jan 24 2010 - 05:10:30 CET

This archive was generated by hypermail 2.2.0 : Sun Jan 24 2010 - 17:50:01 CET