# Why are elementwise additions much faster in separate loops than in a combined loop?

When adding more than 2 arrays, separate loops for adding elements step by step prove to give a better performance than adding the elements in a single loop.

To Test the above statement, two code blocks add the 4 array elements namely array a with b and c with d. The first code block uses a combined loop to add them while the second block uses two different for loops. Clock is used in both the programs for measuring the time taken to execute the loops.

Using Combined Loop

 `// C program to illustrate ` `// performance of loops ` `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``const` `int` `n = 100000; ` ` `  `    ``double``* a1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* b1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* c1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* d1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` ` `  `    ``// Zero the data to prevent any chance of denormals. ` `    ``memset``(a1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(b1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(c1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(d1, 0, n * ``sizeof``(``double``)); ` `    ``clock_t` `start = ``clock``(); ` ` `  `    ``int` `c = 0; ` `    ``while` `(c++ < 10000) { ` `        ``for` `(``int` `j = 0; j < n; j++) { ` `            ``a1[j] += b1[j]; ` `            ``c1[j] += d1[j]; ` `        ``} ` `    ``} ` `    ``clock_t` `end = ``clock``(); ` `    ``cout << ``"seconds = "` `<< (``double``)(end - start) / CLOCKS_PER_SEC << endl; ` ` `  `    ``return` `0; ` `} `

Output :

`seconds = 2.47865`

Using Separate Loop

 `// C program to illustrate ` `// performance of loops ` `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``const` `int` `n = 100000; ` ` `  `    ``double``* a1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* b1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* c1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` `    ``double``* d1 = (``double``*)``malloc``(n * ``sizeof``(``double``)); ` ` `  `    ``// Zero the data to prevent any chance of denormals. ` `    ``memset``(a1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(b1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(c1, 0, n * ``sizeof``(``double``)); ` `    ``memset``(d1, 0, n * ``sizeof``(``double``)); ` `    ``clock_t` `start = ``clock``(); ` ` `  `    ``int` `c = 0; ` ` `  `    ``while` `(c++ < 10000) { ` ` `  `        ``for` `(``int` `j = 0; j < n; j++) { ` `            ``a1[j] += b1[j]; ` `        ``} ` `        ``for` `(``int` `j = 0; j < n; j++) { ` `            ``c1[j] += d1[j]; ` `        ``} ` `    ``} ` ` `  `    ``clock_t` `end = ``clock``(); ` `    ``cout << ``"seconds = "` `<< (``double``)(end - start) / CLOCKS_PER_SEC << endl; ` ` `  `    ``// system("pause"); ` `    ``return` `0; ` `} `

Output :

`seconds = 2.07937`

Note : Actual Output time depends on the compiler used

As we can see from the above examples separate loops are faster in addition than in combined loops. Reason for this is that assuming a simple LIFO caching policy, separate loop would first cause a and b to be loaded into RAM and then be worked on entirely in RAM. When the second loop starts, c and d would then be loaded from disk into RAM and operated on. Thus the arrays are loaded once into the memory.

While the combined loop will page out two arrays and page in the other two every time around the loop. Thus combined loop would be much slower since page in page out has to occur repeatedly compared to separate loops where arrays are loaded once and worked upon.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.