Use std::vector or arrays when filling branches?

Hi all, I am a student entering the data analysis world and perfomance is beginning to be an issue as I am moving towards more and more data.

The generic question is: if one uses vectors instead of arrays how much slower can things get? Are there any clear benefits when using vectors.
If you have absolutely no idea of what the length will be I guess one goes with vectors. But what if you can set a satisfying upper limit and use a big array of which you use only a part every time like described in the manual. Which is the way to go?

Specifically I am getting very long process time on creating a tree wich includes 10 branches using vectors, average length of vector is let’s say 10, tree has 5000 entries . The reason I am asking and not trying it is that the code is a mess and If I go through the pain I want to know if there is any sense in doing it. I tried in simpler cases but I saw no difference because processing was fast in both cases.

p.s. how can I measure the time it takes to run a script, or a function from a program created with .L when using CINT?

Hi,

a C-style vector can have varying length by specifying another member as a comment, which holds the length of the C-style array; see p172 of the User’s Guide.

If you prefer C++ you can use a vector or a vector<T*>. The latter requires far more heap operations (not ROOT’s fault :slight_smile: and is thus considerably slower. The I/O system is also not guaranteed that the elements are all of the same type, ans so it also needs to query each element (for each event) for its type which again costs time. Thus in order of decreasing bandwidth:

T* // [fNumTs]
vector
vector<T*>

Either way, this is really not relevant for 50001010 elements - there must be something that’s really wrong to get large processing times. On Linux you can run valgrind -tool=callgrind to see where the (CPU) time is spent.

You can use gROOT->Time() which initializes timing output after every prompt line; you can reset the timer by calling gROOT->Time() again.

Cheers, Axel.