473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

efficiency and nested loops

My friend has three nested loops, each has 10000, 1000, 100
iterations, respectively.

What should be the most efficient way of layout out the three nested
loops?

for (i=0; i<10000; i++)
for (j=0; j<1000; j++)
for (k=0; k<100; k++)
{

do something;
}
or

for (k=0; k<100; k++)
for (j=0; j<1000; j++)
for (i=0; i<10000; i++)
{

do something;
}

???

Sep 10 '07 #1
3 4402
On Sep 10, 5:42 pm, Luna Moon <lunamoonm...@gmail.comwrote:
My friend has three nested loops, each has 10000, 1000, 100
iterations, respectively.

What should be the most efficient way of layout out the three nested
loops?

for (i=0; i<10000; i++)
for (j=0; j<1000; j++)
for (k=0; k<100; k++)
{

do something;
}

or

for (k=0; k<100; k++)
for (j=0; j<1000; j++)
for (i=0; i<10000; i++)
{

do something;
}

???
Depends on what 'do something' is. If you are iterating through a 3D
array (volume) then you may gain performance by ensuring that the
innermost loop traverses memory with least 'hops'; this will minimise
cache misses.

The same used to be a big deal on paged virtual memory systems.

Otherwise, the advice about premature optimisation is worth noting.

Best regards,

Jon C.

Sep 10 '07 #2
What should be the most efficient way of layout out the three nested
loops?
Does not really make sense (you should spend more time before you post
asking yourself whether your posts are complete and unambiguous). What is
efficient?
The code is the fastest to execute?
The code is the easiest to maintain?

Assuming you mean CPU efficient, it should be such that the inmost loop is
going the memory references that are adjacent. That make it cache friendly
For C and C++, for multi-dimensional arrays, it is the right-most index.

for (i=0; i<10000; i++)
for (j=0; j<1000; j++)
for (k=0; k<100; k++)
array[i][j][k] = i * j * k; // Cache friendly

Also, it is possible sometime to unroll a 3 index loop into a 1-index loop
which can be faster. e.g..

for (i=0; i<10000 * 1000 * 100; i++)
array2[i]= 1;

or replace with a function call like memset() etc.

Stephen Howe
Sep 10 '07 #3
Luna Moon wrote:
My friend has three nested loops, each has 10000, 1000, 100
iterations, respectively.

What should be the most efficient way of layout out the three nested
loops?

for (i=0; i<10000; i++)
for (j=0; j<1000; j++)
for (k=0; k<100; k++)
{

do something;
}
or

for (k=0; k<100; k++)
for (j=0; j<1000; j++)
for (i=0; i<10000; i++)
{

do something;
}

???
Someone already pointed you to Knuth's Law.

To be more precise, though, it depends.

Specifically it depends on:

1) what "do something" means
2) Your CPU
3) Your system's memory architecture
4) Your OS
5) Your compiler and optimization level.

In other words, there is no answer.

Benchmark, BENCHMARK, *BENCHMARK* and then come back if your loop really
is a bottleneck.

Sep 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daan | last post by:
Hello all, I have attempted to write my own forum using PHP and MySQL. It works, but the efficiency is far from ideal. It takes a long time to load e.g. the list with threads in a certain forum....
25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.