[NXC] Optimize this code?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: [NXC] Optimize this code?

Post 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.
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Optimize this code?

Post 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!
Thanks, and have a nice day,
nxtboy III

programnxt.com
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [NXC] Optimize this code?

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Optimize this code?

Post by nxtboyiii »

So is this the fastest it will go?
Maybe using more NBC code will help....
Thanks, and have a nice day,
nxtboy III

programnxt.com
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [NXC] Optimize this code?

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest