DoomFirePSX icon indicating copy to clipboard operation
DoomFirePSX copied to clipboard

loops using postincrement when not needing temporary value

Open sirus20x6 opened this issue 5 years ago • 3 comments

Small performance issue with your for loops. you use postincrement for all of them i++ etc, but what you want is ++i which is preincrement. post increment has an extra copy constructor because it creates a new temporary value, assigns the current value of i to it, then adds 1 to that. You should only use post increment if you have a statement that can use that extra copy. like if you had x = 3; y = x++ + 7; then y would = 10 and x would be 4. if you had: x = 3; y = ++x + 7; then y would = 11 and x would be 4.

since in a for loop you're just incrementing the loop variable you're not using or even able to use that temporary copy so you should use preincrement. also if javascript had a good memset and memcpy it would change quite a few things

sirus20x6 avatar Dec 29 '18 20:12 sirus20x6

Some claim most compiler will optimize away the issue:

https://stackoverflow.com/questions/1546981/post-increment-vs-pre-increment-javascript-optimization

Is there a way to see the code generated by Chrome and Safari ?

On Sat, Dec 29, 2018 at 12:24 PM sirus20x6 [email protected] wrote:

Small performance issue with your for loops. you use postincrement for all of them i++ etc, but what you want is ++i which is preincrement. post increment has an extra copy constructor because it creates a new temporary value, assigns the current value of i to it, then adds 1 to that. You should only use post increment if you have a statement that can use that extra copy. like if you had x = 3; y = x++ + 7; then y would = 10 and x would be 4. if you had: x = 3; y = ++x + 7; then y would = 11 and x would be 4.

since in a for loop you're just incrementing the loop variable you're not using or even able to use that temporary copy so you should use preincrement. also if javascript had a good memset and memcpy it would change quite a few things

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/fabiensanglard/DoomFirePSX/issues/3, or mute the thread https://github.com/notifications/unsubscribe-auth/ABKbkPiKXuyGHCrHfIwzDJMKEbFpRkPiks5u98-ZgaJpZM4ZksyI .

fabiensanglard avatar Dec 30 '18 02:12 fabiensanglard

I wonder if you profile it, will there be a significant difference?

KosayJabre avatar Dec 30 '18 18:12 KosayJabre

it could be that it gets optimized away. it also could be that it gets optimized away and in a later build it wont and again in a later build it will be again. it's just good practice.

Also the article that brought me here was about how tight programming made a doom port awesome so it only makes sense to try and be performant even if you are in javascript.

The difference probably isn't significant in the sense that most people have 4+ core cpus and gigs of ram, but as far as the example goes it makes sense to write it preincrement. by writing it post increment you're sort of idiomatically saying I want to use this variable for something and then increment it which is not what happens. by writing it preincrement you're implying I just want this variable to go up by one and I don't need the preincrement value.

sirus20x6 avatar Dec 31 '18 04:12 sirus20x6