-
mouse movement
Hi I have a piece of code here for a mouse to eat food on the maze and choose which direction.
Any1 have suggestion how can I modify the code, so the progrom runs faster and I want to simplify the code as much as possible.
An other action of the mouse I should add?
thanks
Code:
void evaltree(void)
{ if (pellets == 0 || steps > MAX_STEPS) return;
switch (*nodep++)
{ case LEFT: heading = (heading==NORTH? WEST : heading-1);
record_turn();
steps++;
return;
case RIGHT: heading = (heading+1) % 4;
record_turn();
steps++;
return;
case MOVE: switch (heading)
{ case NORTH: if (row == 0) row = MATRIX_SIZE;
row--;
break;
case EAST: col = (col+1) % MATRIX_SIZE;
break;
case SOUTH: row = (row+1) % MATRIX_SIZE;
break;
case WEST: if (col == 0) col = MATRIX_SIZE;
col--;
break;
}
if (trace)
{ //(AG) Old trace removed - add record move.
//trace_matrix[row][col] = 2;
//print_matrix();
//puts("Continue trace?");
//trace = (getch() == 'y');
fprintf(filep, "<step>\n");
fprintf(filep, "<automata_section>\n");
fprintf(filep, "<automata automaton_id=\"0\" shape=\"ant\" name=\"ANT\" >\n");
fprintf(filep, "<location loc_x=\"%d\" loc_y=\"%d\"/>\n", col, (31 - row));
fprintf(filep, "</automata>\n");
fprintf(filep, "</automata_section>\n");
fprintf(filep, "<cell_section>\n");
}
if (matrix[row][col])
{ matrix[row][col] = 0;
pellets--;
//(AG) Record cell eaten
if (trace)
{
fprintf(filep, "<cell state=\"eaten\" loc_x=\"%d\" loc_y=\"%d\"/>\n", col, (31 - row));
}
}
//(AG) Close cell section and step
if (trace)
{
fprintf(filep, "</cell_section>\n");
fprintf(filep, "</step>\n");
}
steps++;
return;
case IF_FOOD_AHEAD:
switch (heading)
{ case NORTH: next_row = (row == 0? MATRIX_SIZE-1: row-1);
next_col = col;
break;
case EAST: next_row = row;
next_col = (col+1) % MATRIX_SIZE;
break;
case SOUTH: next_row = (row+1) % MATRIX_SIZE;
next_col = col;
break;
case WEST: next_row = row;
next_col = (col == 0? MATRIX_SIZE-1: col-1);
break;
}
if (matrix[next_row][next_col])
{ evaltree();
skip_arg();
}
else
{ skip_arg();
evaltree();
}
return;
case PROGN2: evaltree(); evaltree(); return;
case PROGN3: evaltree(); evaltree(); evaltree(); return;
}
}
void test_fitness(int index)
{
// Evaluate fitness of the indexed individual
struct prog *progp = &population[index];
int count = progp->proglen;
// Initialise the trail matrix
memcpy(matrix, matrix_data, MATRIX_SIZE*MATRIX_SIZE);
-
Run a profiler if there are any speed issues. It will find bottlenecks.
-
How about can i simply the code, but carry out the same result?
thanks
-
That's what the suggestion Desolation made was for: find out what's taking the time, then look to simplify that, instead of just trying to work on random pieces of the code.
I don't see much inefficiency, and while this may be slow I don't know that it's going to get better, because I see lots of file i/o, and that is approximately 75346.82 times slower than anything else. The only possible weird thing is the recursion in evaltree() calling itself 2 or three times as though this probably wasn't already in some big loop. Edit to add: although since there don't appear to be any local variables, or any parameters, it should be really easy to push the stack, so that won't help (or hurt).
-
Thanks
So what can i modify for the moves?
I really want to simplify the code, any1 can help me please
thanks
-
Who knows what you can modify? You're the one with all the code. The snippet you posted seems fine. Did you make sure your optimizations were turned up all the way (-O3 or similar)?
But rather than posting random snippets of your code onto random message boards, time your code. Are you using GNU compilers? If so, look at
http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html
(The manual seems old, but it's probably still all right.)
-
I am sorry
I know i asked some question before, but I have solve the random maze on my own.
I was looking to simplify the code into 3-4 line shorter.
I am very sorry