The first thing in optimizing code is always measuring: Find out where it runs slowly. It's often not where you think. Old rule:
Measure twice, optimize once.
I'm not only posting this because it's true, but also because I'd be really happy if you were willing to give my NXC Profiler a spin:
http://www.mindstorms.rwth-aachen.de/tr ... XCProfiler
Unfortunately the NXTasy post with a sort-of manual is gone, but you could basically do something like this. In your task Game:
Code: Select all
// SET UP THE PROFILER
// comment this out to silently disable profiling without any side effects!
#define PROFILER_ENABLE
// how many times did we use PROFILER_BEGINSECTION (and ENDSECTION)
#define PROFILER_MAXSECTIONS 7
// name of the binary results file we later analyze
#define PROFILER_RESULTSFILE "game.prf"
// this does all the magic :-)
#include "Profiler01.nxc"
task Game()
{
PROFILER_START();
while(1)
{
while(play == 1)
{
paddx=fmapx == floor(fmapx)?0:1;
if(SENSOR_2 && SENSOR_1)
{
StopAllTasks();
}
ShootFireBall();
Jump();
mapy=floor(fmapy);
mapx=floor(fmapx);
paddy=fmapy == floor(fmapy)?0:1;
paddx=fmapx == floor(fmapx)?0:1;
MoveLeft();
mapy=floor(fmapy);
mapx=floor(fmapx);
if(fmapy == floor(fmapy))
{
paddy=0;
}
else
{
paddy=1;
}
PROFILER_BEGINSECTION(0);
MoveRight();
PROFILER_ENDSECTION(0);
mapy=floor(fmapy);
mapx=floor(fmapx);
paddx=fmapx == floor(fmapx)?0:1;
if(!ButtonPressed(BTNCENTER,0) && jc != 0)
{
ffd=1;
jumping=0;
}
PROFILER_BEGINSECTION(1);
FireBallHit();
PROFILER_ENDSECTION(1);
PROFILER_BEGINSECTION(2);
Down();
PROFILER_ENDSECTION(2);
s=0;
mapy=floor(fmapy);
mapx=floor(fmapx);
CheckVargs();
Tile();
CheckFireBall();
fballx+=xpp;
fbally+=ypp;
//********************************************************************************************************************
// [snipped]
//********************************************************************************************************************
// [snipped]
}
PlayToneEx(600,30,1,0);
PROFILER_STOP();
}
}
As you can see, I added some #define, an #include, and then the profiler-commands. All you have to make sure is, that somewhere in that file, PROFILER_START() gets called once before the other profiler commands. At the end, somewhere, you
have to call PROFILER_STOP() once, too. Otherwise it won't work. I'm not sure if I chose the right position in your code...
The main thing is then to mark sections that should be profiled. Set how many sections you have at max with
in the beginning.
Then you have to manually wrap every line or section of code with PROFILER_BEGINSECTION(n) and PROFILER_ENDSECTION(n), where n is a constant integer expression. I think you don't have to have the sections "in order", but it's all an early alpha version, so better do that. You should also make sure that both commands are in the same scoping level, i.e. you begin and end a section within the same "code flow" (for example, avoid having an end-section outside some if-statement that is not reached as often as the begin-section).
That should be it. You should run the program and stop it gracefully (so that PROFILER_STOP() gets executed). You should then find a file called "game.prf" on your NXT. Copy it to your computer, and run the Python script to generate a highlighted HTML version of your code. If you can't get it to work, send me the .prf file, and I'll do it.
You can of course wrap the stuff you marked with //*************** in your code in between profiler sections, I just didn't do it to save code space here.
You have to download Profiler01.nxc and put it to your projects directory, here is a
direct download link:
http://www.mindstorms.rwth-aachen.de/tr ... iler01.nxc
The profiled program might run slower (up to a factor of 2 to 10), this is normal. You can leave in all the profiler commands and just not #define PROFILER_ENABLE. If you do this, all profiler-commands evaluate to empty macros and should have no effects at all.
More things and an example is described on the project page I linked above.
Sorry if this kind of hijacked your thread, but I can think together with the NXC-experts here it could seriously help speed up you program -- once we know where it's slow
Edit: I should note this whole stuff only works for ONE NXC file at the moment. And I guess all profiler section commands should be in one single task. This is all given for your game task.