Since such optimizations can be made, I'll say no myself.
What are you talking about. =|
Krispy Kreme said:
Since such optimizations can be made, I'll say no myself. What are you talking about. =| |
It's not as much optimization as it is...meh. Refactoring, I guess I could say. The reason he needed goto was because his design was flawed to begin with.
|
Android Data wrote:
I'm curious. Would the code below (which I just created but use a similar version of in other code) be considered a good way of using goto? No; And as mentioned, if you have to ask yourself, it isn't. > pword Non-goto version, far cleaner and simpler: mob/verb/pwords() |
Alathon wrote:
Android Data wrote: if(ckey=="androiddata"||(ckey in allowed)) Small fix. I misinterpreted it. I ment something like this: pword |
Same deal. These sorts of situations, no matter the minor detail here, really aren't suited for goto and you don't even need a nested loop here.
pword/var |
I kind of wonder about these results, whether extended trials would give you different results (as they well might). I don't mean longer loops, but rather just doing the three routines in sequence again and again--or maybe even randomizing their order. Of course, you also have to fix your loops because in One() and Two() you're really only showing the numbers 2 through 5000, whereas in Three() you show 1 through 5000.
If goto is taking longer, I suspect a syntax error in your code may be to blame. You have a colon in the goto statement that is not needed. I'm not sure how the loops are implemented internally, but I'd be very surprised if it doesn't boil down to a goto instruction just as when C compiles to assembly. That being the case, your version with goto should always be faster because it's marginally more efficient. The assumption doesn't apply quite as well to interpreted languages as to machine code, but still it has some validity. If DM compiles anything like C, a for() loop is implemented like this: var/i=1 ...which translates to: var/i=1 In C, at least in older compilers (newer ones may use this optimization), you can eliminate one of the goto statements each time around by "flipping" the loop. A flipped loop is a do-while instead of a while. In C you'd implement it (somewhat hackily) like so: var/i In a goto format, it looks like this: var/i In machine code, this is a huge advantage because all modern processors use prefetching. They grab instructions by looking ahead to where the instruction pointer will be pointing next, thereby reducing the number of slower memory access operations. Prefetching only works if it knows where to look, so for absolute jumps it always knows where to fetch the next instructions. For conditional jumps, it has to guess based on how the loop is likely to go; if it guesses wrong, it has to throw out the instructions and fetch new ones. The upshot is that an unflipped loop has two gotos every loop; a flipped loop has just one. Even in interpreted code, a flipped loop should be marginally faster based on its simplicity. Lummox JR |
Since such optimizations can be made, I'll say no myself.