I've setup a small program to measure the performance of the netduino:
public class Program
{
static bool state = false;
static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
static AnalogInput input1 = new AnalogInput(Pins.GPIO_PIN_A0);
static int analogValue = 0;
static bool ledstate = false;
static int counter = 0;
public static void Main()
{
long start = DateTime.Now.Ticks;
for (int i = 0; i < 2500; i++)
{
analogValue = input1.Read();
ledstate = !ledstate;
counter = counter + 1;
led.Write(ledstate);
}
long end = DateTime.Now.Ticks;
TimeSpan span = new TimeSpan(end - start);
Debug.Print("timespan: " +span.Milliseconds.ToString() + "ms");
}
}
If I run this code, it reports that it took 37ms. But if I comment out the counter = counter + 1
part it reports that it took 979ms !! Thats strange right?
I increased the for to about 20000, then it reports it took 900ms, but the led is way longer on than a second. So it simply reports the wrong time. But why? Can't figure it out.
I'm not sure about the cpu that's on the netduino, but most modern cpu's will queue up instructions in their own order as long as they don't screw up dependencies. So your counter may be irrelevant to the actual performance of the code (especially since nothing depends on it).
For example, a multiplication may take 30 cycles to complete, where as an addition will probably only take 1.
Ex.
counter ++;
othernumber *= 3;
counter = counter + othernumber;
if your cpu ran this exactly how it was written it would go like this
counter++; //1 cycle til complete
othernumber *= 3; //30 cycles til multiplication completes
//29-0 NOOP aka no operation (waiting on multiplication to complete)
counter = counter + othernumber; //now we can do this at the 32 cycle mark
your machine code might swap the order of those two since they run in different area's of the cpu (the adder and the multiplier)
it might do
othernumber *= 3; //30 cycles til multiplication complete
counter++; //29 cycles til multiplication complete
//28-0 NOOP aka no operation (waiting on multiplication to complete)
counter = counter + othernumber;//now we can do this at the 31 cycle mark
What I'm trying to say is your counter is probably irrelevant, and performance of the netduino based on the speed the light can turn off and on will probably be very skewed
Homer: [Meeting Aliens] Please don't eat me! I have a wife and kids. Eat them!