nested-loops

Replace Nested For Loops… or not

情到浓时终转凉″ 提交于 2020-12-05 07:13:21
问题 I have a script that loops through a series of four (or less) characters strings. For example: aaaa aaab aaac aaad If have been able to implement it with nested for loops like so: chars = string.digits + string.uppercase + string.lowercase for a in chars: print '%s' % a for b in chars: print '%s%s' % (a, b) for c in chars: print '%s%s%s' % (a, b, c) for d in chars: print '%s%s%s%s' % (a, b, c, d) Is this sort of loop nesting a bad thing, and if so, what would be a better way of accomplishing

A big loop within a small loop always faster than a small loop within a big one?

一个人想着一个人 提交于 2020-08-02 08:23:08
问题 I just read this post, and wonder if we can draw the conclusion that a big loop within a small loop must always run faster than a small loop within a big one, no matter what the code does inside the nested loop? Take an example. int m, n; m = 1000000; n = 10; Snippet A for (int i = 0; i < n; i++) for (int j=0; j < m; j++) { DoSomething(); } Snippet B for (int j = 0; j < m; j++) for (int i=0; i < n; i++) { DoSomething(); } Can we say that, no matter what DoSomething() actually does, snippet A