How to make the loop faster?

Hi Rooter,
I am a new user of root and I wander how to make my loop faster?
For example I have a FOR loop in order to fit a set of points with a line.
500,000,000 groups of data and each group has 4 points.
I want to fit each 4 points with a line.
The fitting of each group is independent.
for(long long i=0;i<500000000;i++){

So I want to know how to make this loop faster if I don’t use TThread?
Thanks a lot!

Please provide the following information:

ROOT Version (e.g. 6.12/02): root 5.34/28
Platform, compiler (e.g. CentOS 7.3, gcc6.2): linux

there are two reasonably simple ways you can make a loop faster: do less work inside, or distribute the loop to several workers which process chunks of your range separately.

In your case it looks like the task executed inside the loop is trivially parallelizable.
ROOT offers TThreadExecutor and TProcessExecutor to distribute the workload between threads and processes respectively (roughly speaking, the difference is that threads share memory, i.e. a given variable x will be common to all threads, while processes do not, each process gets its own copy of variable x – threads are usually more performant but the memory sharing can make writing correct code very tricky).

In your case, this:

std::vector<FourPoints> toFit = MakeVectorOfPointsToFit();
std::vector<FitResult> fitResults;
for (long long i=0; i < 500000000; ++i) {
   auto fitResult = Fit(toFit[i]);

would be rewritten like this

std::vector<FourPoints> toFit = MakeVectorOfPointsToFit();
ROOT::TThreadExecutor pool; // or ROOT::TProcessExecutor
auto fitResults = pool.Map([](Point p) { return Fit(p); }, toFit);

Hope this helps,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.