StepCount=long(MaxDeltaPos);
time1=micros();
for (unsigned long n=1; n <= StepCount; n++)
{
for (int a=0; a <=NumberOfAxis; a++)
{
Step[a]= int(n * StepRatio[a])- int((n - 1) * StepRatio[a]);
DoStep +=byte(Step[a]*pow(2,a));
}
PORTA=DoStep; // slow 2 milli seconds
PORTA=B11111; //fast
PORTA=ZeroPins; //fast
DoStep=0;
}
time2=micros();
time3=time2-time1;
Serial.println(time3/StepCount);
}

DoStep is a byte that contains a number from 0 to 255 but runs slow it take 2000 microsteps for each loop. If I coment it out (//PORTA=DoStep; ) then it takes 1 microsecond to run each loop.

Ok, i'm definitely talking through my hat here but without that dostep statement nothing really changes in that loop. The compiler could just decide you only need to do it once and save you some time.

It's not obvious how many times each loop runs but you could try simplifying the inner loop. I assume your real interest is in speeding this up?

OK, I updated my code. It does work faster shifting the bits, but it is still 200 microseconds per loop. Is this as good as it gets or is there more optimization?

StepRatio is a double…I could multiply by 1000 and make it a long???

Anymore ideas?

Thanks Coding Badly…Much better way of doing it!

StepCount=long(MaxDeltaPos);
time1=micros();
unsigned int a=0;
for (unsigned long n=1; n <= StepCount; n++)
{
for (a=0; a <=NumberOfAxis; a++)
{
Step +=(int(n * StepRatio[a])- int((n - 1) * StepRatio[a])) << a;
}
// Serial.println(Step,BIN);
PORTA=Step;
delayMicroseconds(5);
PORTA=ZeroPins;
Step=0;
}
time2=micros();
time3=time2-time1;
Serial.println(time3/MaxDeltaPos);
}

start with the float, that is easy and will make a big difference.

just looking at the code, how come step is zeroed between loops but not before the 1st one? How come the results for the diferent axes are all dropped into PORTA?

If the stepcount thing doesn't have a big range I think you should be looking at a table lookup.

since it is bigger than 1, I need the integer part of this. Is there a fast way of getting the integer part of val?

I think you have to get rid of any floats. I should have realized StepRatio was one.

What if you premultiplied stepratio by some power of 2 - say 256 then get the int by shifting bits off the bottom. So instead of .333 it's 85. 85*4=340, 340>>8 will be the "1" you're looking for without the floats.

It may seem evil but if the above gets you somewhere but not far enought try straightening out that inner loop so you repeat it 6 times and code the stepratios as constants.

Instead of floating-point multiply by 0.33, why not do integer math with divide by 3?

int x = 4;
int val;
val = x/3; // integer division, result is 1

You'll have to go through a bit of work to convert your step ratios to integer numbers, but if you can eliminate floating-point math you'll get rid of a lot of processing time.

Instead of floating-point multiply by 0.33, why not do integer math with divide by 3?

good thought. In fact, if the ratios were close enough to 1/n you could do the whole step detection with modulo arithmetic. i.e. if stepratio were 1/3 then

((n%3)==1)<<a;
could replace the whole RHS of
Step +=(int(n * StepRatio[a])- int((n - 1) * StepRatio[a])) << a;

If the stepratios won’t quite cooperate there must be something there that would work.