tags:

views:

288

answers:

6

Recently, I want to optimize my code. Would any one tell how to speed up looping? While-loop is faster than for-loop? And which type of for-loop is faster?

It is surely I am not talking about a empty content looping. I mean If any good way to loop a huge data in Java or Python.

And I got the answer from here

+9  A: 

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified”

faster at what looping? what is in the loop is what takes the time, a loop that does nothing is the fastest ... this question makes no sense.

the lesson here is that the actual instructions for the looping construct are sub-microseconds and highly optimized already in all the languages you mention ( Java, Python and C ( with a modern compiler ) ).

what is going on in your loop is going to have way more impact than the few instructions that any of the looping constructs will have. If your code is 100,000 instructions, do you really care if one loop construct takes 3 instructions and another takes 2?

fuzzy lollipop
I feel like this quote gets bandied about so much. It's really unfortunate and I think it is misused a lot. However, it is become the most popular mantra that performance is irrelevant and no one bothers optimizing code anymore.
BobbyShaftoe
@BobbyShaftoe: If you read this as "don't optimize code" then you need, perhaps, some remedial reading comprehension courses. Hint: "... [it is] wise to look carefully at the critical code; but only **after that code has been identified** ..." (emphasis mine)
JUST MY correct OPINION
Nonsense, I don't have any trouble with reading comprehension. Search any performance question on SO and you see the same party line being espoused. I never said Knuth was incorrect. I said quoting him in this way to shun performance optimization is misusing it. Maybe you should take those same courses.
BobbyShaftoe
@Bobby you do have a problem with reading for comprehension, the word __"premature"__ is the key to the entire idea that he is trying to convey. And he says that it __is__ important, but only after identifying code that is not up to snuff. I agree if you think this quote is espousing "shunning" optimization you don't understand the meaning behind the quote.
fuzzy lollipop
@fuzzy-lollipop, clearly you also have a problem with this reading comprehension thing. How many times have I written that I don't quarrel with Knuth's original quote as intended? You are missing the entire context of the discussion. I've lost count of the number of questions I've come to that ask about specific perf. optimization techniques that get relegated to "premature optimization" because that is paritularly vogue these days. Sure, follow Amdahl's Law, but that doesn't mean we shouldn't care about performance and undersand how to do things like loop optimization. Thanks for playing.
BobbyShaftoe
I started programming in 1983, I was against premature optimization without profiling even way back in the day. When I was writing 6502 ASM code for demos that needed every operation and cycle to count, something like loop optimization was valid. But even then I did what I could in BASIC and only refactored to ASM after profiling. That was WAY before what you are calling "in vogue" today. Today, with multicore 3+ GHZ processors, outside of very esoteric scientific applications espousing that someone do loop optimization is a complete disservice to a beginner.
fuzzy lollipop
If you want to really help beginners, and this is a beginner question. Then you should be promoting getting code to work CORRECTLY, fast code that is wrong is still wrong. Then getting it to be MAINTAINABLE, fast code that is esoteric and can't be easily maintained will just get re-written, and then once your code is CORRECT, and UNDERSTANDABLE, go and for some reason something isn't "fast enough" then profile it and only optimize what you find is "slow". Claiming that loop optimization is valid in Java or Python and even C with a modern compiler is completely disingenuous as well.
fuzzy lollipop
@fuzzy lollipop, I was going to let it go but it's a bit uneducated to suggest that loop optimization is no longer valid with modern compilers. So if you were going to write a subroutine to perform sparse matrix multiplication, would you write three simple nested loops and be done with it?
BobbyShaftoe
again read for comprehension I stated above "...outside of very esoteric scientific applications...", which easily falls into Knuths 3%. And 3 nested loops is a naive implementation of the __algorithm__ associated with sparse matrix manipulation, which isn't by any stretch of the imagination related to whether a for loop or while loop is faster than the other. It is an algorithm problem, stay on point. You are making my point for me, how fast a "loop" executes is irrelevant to what is inside the loop.
fuzzy lollipop
+6  A: 

The actual loop is not what's going to speed up your code.

You should find better algorithms for what you're doing IN the loop, and go from there.

Also, don't forget that you should only optimize your code if it's too slow for your purposes!

There's a little more here, if you're interested.

Chris Cooper
Actually no, loop optimization is a very valid and well studied technique.
BobbyShaftoe
@Bobby: I'm not suggesting it isn't, but the OP's criteria certainly isn't the first thing you should look at. Certainly not if your only criteria is whether a for-loop or while-loop is faster.
Chris Cooper
+1  A: 

While- and for-loops are the same thing at runtime. I'm not sure what you mean by "type" of for-loop.

Your questions pre-supposes that the looping construct itself takes up significant time. That is extremely unlikely - it's the body of the loop that would take time. You should profile your code to see exactly where the bottlenecks are and then optimise those.

Evgeny
+1  A: 

The fastest loop is the one that does nothing.

To optimize your application you first have to:

  • have it working properly.

Then

  • use a profiler to identify bottlenecks

And finally

  • improve the detected spots. Most of the times using a new algorithm will do.

As for which is faster for or while loops, they are equivalent so don't look there.

If you have an specific problem we could help you. For a Java sample on how to use Java VisualVM you can see this other question:Trying to solve 15 Puzzle, OutOfMemoryError

OscarRyz
+3  A: 

In any of these languages:

for(int i = 0; i < n; ++i) {
   doSomethingWith(i);
}

is equivalent to:

int i = 0;
while(i < n) {
   doSomethingWith(i);
   ++i;
}

That said, there are techniques for optimizing loops, for example loop unrolling:

for(int i = 0; i < n; i += 4) {
   doSomethingWith(i);
   doSomethingWith(i+1);
   doSomethingWith(i+2);
   doSomethingWith(i+3);
}

However, in almost all cases it is best to let the compiler do it for you automatically.

Bottom line: I concur with the general opinions expressed here. First make sure that what is in the loop is the bottleneck. If it is, try to optimize what is in the loop.

Andrew Stein
It's still important to think about loop order, not all compilers (in fact hardly any) will fix a problem like accessing a row-major matrix in a column-major order and the penalty for doing something like that is *incredibly* high.
Mark E
@Mark, you are quite correct.
Andrew Stein
+1 for actually getting at something interesting rather than reiterating cliches about attempting to make code actually perform well.
BobbyShaftoe
+1  A: 

while(1) {} / for (;;); / do {} while(1); is clearly the fastest, on x86 it will assemble to:

label:
jmp label

0/0 (100%) work per clock cycle, pretty good if you ask me

Longpoke
While your loops iterate pretty fast, they still take a long time to run. It can be optimizedwhile(0) { } / for ( ; false ; ) / do { } while false
emory