Topic List |
Page List:
1 |
---|---|
Yellow 02/07/20 1:52:21 AM #1: |
I never, never see this done this way, but it seems like the fastest way to do a for loop.
Instead of for (int i = 0; i < something; i++) { ... } for (int i = 0; i++ < something;) { ... } I heard adding ++ to the end of an integer while reading basically has no performance loss. Doing it this way basically removes an opcode from every loop, right? I know for loops produce less garbage than foreach. --- ... Copied to Clipboard!
|
Sahuagin 02/07/20 2:32:44 AM #2: |
readability is more important than typing slightly less. a rule of thumb is to never use ++, especially combining it with evaluation.
for (int x = 0; x < length; x += 1) { } one exception I allow myself is when you have a list of array accesses that all reference sequentially. var x = -1; Object.Property = values[++x]; Object.AnotherProperty = values[++x]; // etc... it's less error prone not to actually type sequential integers. can also use a closure: var x = -1; Something next() => values[++x]; Object.Property = next(); Object.AnotherProperty = next(); (there's also a reason why I use ++x instead of x++ but can't remember.) Yellow posted... Doing it this way basically removes an opcode from every loop, right?I don't think so, how could it? what opcode would be removed? always going to be something like: mov ax, 0 loop: cmp ax, length jl done <body> inc ax jmp loop done: might be able to eliminate a jump moving things around but I think that's the order things occur with a for loop. --- ... Copied to Clipboard!
|
YoukaiSlayer 02/07/20 4:27:09 AM #3: |
I thought performance wise it was faster to iterate backwards.
So like: for (int i = something.length; i-- > 0;) { ... } I think the idea is that you don't want to reference a property in the comparison that has to be done over and over versus assignment that is only performed once. Unless the array is massive or you have to call this a lot of times per second it probably doesn't matter and readability matters more. I'm not an expert though and just going from what I'd read elsewhere. --- I'm ninja (you can't see me) ... Copied to Clipboard!
|
Amuseum 02/07/20 2:16:37 PM #4: |
Yellow posted...
I never, never see this done this way, but it seems like the fastest way to do a for loop. youre skipping 0 index, and will get index out of range error at the end of the loop. anyway local variables are faster generally. Ex. for (int i =0, int n =myarray.length; i < n; i++) Besides that, generally good compilers and interpreters already know how to optimize loops for you. There's a site that lets you compare different types of loops for javascript. including for vs foreach. For that matter, Browsers keep getting better at speeding up foreach loops. --- Ergonomic keyboard layouts for Android https://goo.gl/KR1vK6 Shena'Fu's Online Card Creator https://bit.ly/sfocc ... Copied to Clipboard!
|
Lokarin 02/07/20 2:26:55 PM #5: |
is using a while...wend statement the same speed as for/next?
--- "Salt cures Everything!" My YouTube: https://www.youtube.com/user/Nirakolov/videos ... Copied to Clipboard!
|
TheWorstPoster 02/07/20 2:39:37 PM #6: |
Yellow posted...
I never, never see this done this way, but it seems like the fastest way to do a for loop. Just do the top one for legibility reasons ... Copied to Clipboard!
|
Sahuagin 02/07/20 8:10:42 PM #7: |
Amuseum posted...
youre skipping 0 index, and will get index out of range error at the end of the loop.I should have noticed that. I guess it depends if you're using i for something, but yeah, i will count from 1 to something, not 0 to something - 1. this kind of performance optimization is usually completely unnecessary, unless you very specifically have to make this one loop as fast as possible. modern computers are already unfathomably fast, so micro-optimizations are a waste of time. instead, you write your program cleanly, and then if it's slow somewhere, profile it and optimize the part that's actually slow. Lokarin posted... is using a while...wend statement the same speed as for/next?basically (if you hook it up the same way to count). it's do...while that's a bit different since it will always execute the body once. --- ... Copied to Clipboard!
|
#8 | Post #8 was unavailable or deleted. |
Topic List |
Page List:
1 |