How nested loops affect programs' execution ?

As i was asked to write a program to generate all possible anagram for a given word , the logic that has struck my mind at first was using nested loops but i didnt care about execution time and complexity . So i want to be clear if nested loops affect programs' execution badly .

Replies

  • rahul69
    rahul69
    Alok mishra
    As i was asked to write a program to generate all possible anagram for a given word , the logic that has struck my mind at first was using nested loops but i didnt care about execution time and complexity . So i want to be clear if nested loops affect programs' execution badly .
    It depends on the no of times the loop runs, and the amount of nesting,(of course not considering hardware related factors) and if both these parameters are large, it really affects the execution time badly.
  • Anoop Kumar
    Anoop Kumar
    Yes, Loops add complexity and execution time very badly specially nested loops. They should be avoided as much as possible, using collections (Data structure containers).
    Here complexity is Cyclomatic complexity, which is in one line - Number of independent paths in programs.

    Loops/nested doesn't much add in complexity but performance is degraded to worse. while nested looping you must consider what data you want and when to break the loop.

    See the following code, do you find any execution difference between them? which one you will go?

    for(int i = 0; i < l.length; i++) {
        for(int j = 0; j <= i; j++) {
            //your code
        }
    }
    
    
    and :
    
    for(int i = 0; i < l.length; i++) {
        for(int j = 0; j < l.length; j++) {
            //your code
        }
    }
  • Jeffrey Arulraj
    Jeffrey Arulraj
    @#-Link-Snipped-# the first snippet seems better coded for me is that the correct

    Cos I am reducing iterations in the inner loop.

    But based on the need don't you think it will vary a lot
  • Alok mishra
    Alok mishra
    ianoop
    Yes, Loops add complexity and execution time very badly specially nested loops. They should be avoided as much as possible, using collections (Data structure containers).
    Here complexity is Cyclomatic complexity, which is in one line - Number of independent paths in programs.

    Loops/nested doesn't much add in complexity but performance is degraded to worse. while nested looping you must consider what data you want and when to break the loop.

    See the following code, do you find any execution difference between them? which one you will go?
    the first one !! but why did you ask this ?
  • Anoop Kumar
    Anoop Kumar
    Yes, 1st one is more efficient. I asked because whenever looping , be precise how many iteration it gonna take.
  • Alok mishra
    Alok mishra
    ianoop
    Yes, Loops add complexity and execution time very badly specially nested loops. They should be avoided as much as possible, using collections (Data structure containers).
    the first one !!
    ianoop
    Yes, 1st one is more efficient. I asked because whenever looping , be precise how many iteration it gonna take.
    if i say i am using 8 nested loops in a program , does it sound impractical (not correct word) or what would you suggest about that ?
  • Anoop Kumar
    Anoop Kumar
    Even Nested loop is not favorable. More than one nested loop is quite a overhead.
  • KenJackson
    KenJackson
    If you're comparing every combination of 8 characters using all 26 letters by brute force, you'll have to make 26^8 or 2.09E11 comparisons. That's 209 billion inner loops.

    A different approach might be to only compare the words that appear in some dictionary, for example the file /usr/share/dict/words which is probably distributed with every distro of Linux and many other OSes. My copy has 479,828 words--one word per line. (Coincidentally, that's roughtly the square root of 26^8.)

    So comparing every line in that file would result in a single loop with a tiny fraction of the total comparisons.

You are reading an archived discussion.

Related Posts

please can any one tell the design process of Audio amplifier ..any data or pdf regarding to it i am unable to get audio and video systems book by r...
After much thought and comparison between point and shoot cameras and DSLRs, and thanks to the suggestions put forth by CEans on a previous thread, I've decided to invest on...
"Couldn't update to Windows 8.1 - 0xC1900101 - 0x40017"
Back in September 2013, the Blackberry Boys unveiled the all new smartphone Blackberry Z30 in an event in Malaysia and now as promised, the phone is going to be here...
Lenovo has brought to the market a new convertible laptop called Lenovo A10 - which is the company's first to run on Android 4.2 Jelly Bean OS. At 1 kg...