Page 2 of 2

Re: [NXC] Optimize this code?

Posted: 01 Feb 2012, 22:05
by mcsummation
A couple of things:
1) Before you ask someone to "optimize some code", you ought to make it properly formatted. I had to do that just to figure out what was going on.
2) Without doing the actual optimization, I tried running the compiler at Opt=6. That shortened the code some.
3) Again, without doing any optimization, it won't run with more than 105 particles. I get a "File Error", with could mean either a bad index, or, more likely, out of memory. The error is down in the main loop somewhere.
4) Indexing into a 2D array (which isn't a 2^n square) is costly. An awful lot of code is done computing the actual byte that you want. There just isn't much that can be done to optimize that code, other than what the compiler is already doing.

Re: [NXC] Optimize this code?

Posted: 01 Feb 2012, 23:03
by nxtboyiii
Ok, so I tried out the new code and it runs a ton faster!
Thanks so much!

EDIT: Now it runs like, twice as fast!

Re: [NXC] Optimize this code?

Posted: 02 Feb 2012, 00:29
by spillerrec
mcsummation wrote:3) Again, without doing any optimization, it won't run with more than 105 particles. I get a "File Error", with could mean either a bad index, or, more likely, out of memory. The error is down in the main loop somewhere.
The code apparently isn't doing any bounds checking, so it crashes if a particle is at the edge or outside the screen. When you raises the amount of particles it will add the extra particles higher on the screen, so this is likely what you are experiencing.
It is not optimal, but considering the speed impact of bound checking it might be more useful to make sure that the particles cannot fall out of the screen...
mcsummation wrote:4) Indexing into a 2D array (which isn't a 2^n square) is costly. An awful lot of code is done computing the actual byte that you want. There just isn't much that can be done to optimize that code, other than what the compiler is already doing.
Just to make sure you know, arrays in NXC is nothing like arrays in C. If you look at the optimized code I submitted, I mainly changed the way the arrays were accessed and this made a major difference in performance.

Re: [NXC] Optimize this code?

Posted: 11 Feb 2012, 06:26
by nxtboyiii
So is this the fastest it will go?
Maybe using more NBC code will help....

Re: [NXC] Optimize this code?

Posted: 11 Feb 2012, 17:39
by spillerrec
If you convert the if control structures to NBC it will get faster, but I'm not quite sure how much. I dislike writing code in NBC however (especially control structures), so unless I see an example which has a practical use and which runs too slowly to achieve this I'm not even considering to do it.