Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Measuring performance.. strange behaviour


  • Please log in to reply
4 replies to this topic

#1 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 15 August 2010 - 10:36 AM

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.

#2 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 15 August 2010 - 02:33 PM

Is there a TimeSpan.TotalMilleseconds like there is in the full .Net Framework? If not you'll need to add all of the components of the timespan after you multiply them out to get the total.

#3 kingpin2k

kingpin2k

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationUSA

Posted 15 August 2010 - 03:37 PM

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!

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 August 2010 - 04:27 PM

Three quick things:

TimeSpan
As Stephen said, TimeSpan.Milliseconds only returns the Milliseconds component of a TimeSpan. So 1.030s and 4.030s would be return "30".

Execution Time
Different commands take different amounts of time to execute. For example, i++ is pretty quick...but AnalogInput.Read() waits for a hold time, sample time, etc. before returning. A good way to measure execution time of a specific function is:

First, measure your base case... Including the dummyCounter++ ensures that the .NET optimizer does not just skip your loop and set i to 1000 :)

            int dummyCounter = 0;

            // start timer here...

            for (int i = 0; i < 1000; i++) 
            { 
                dummyCounter++;
            } 

            // end timer here...

...then, add your command to measure:

            int dummyCounter = 0;

            // start timer here...

            for (int i = 0; i < 1000; i++) 
            { 
                led.Write(true); // command to measure            
                dummyCounter++;
            } 

            // end timer here...

...and then simply subtract the first time from the second time to get your execution time for 1000 cycles of the command.

How to Time
What you really want is a Stopwatch class. Here's one we've written.

System.Diagnostics.Stopwatch class (code compatible with desktop/mobile C# projects):
http://forums.netdui...topwatch-class/

Chris

#5 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 15 August 2010 - 04:49 PM

Is there a TimeSpan.TotalMilleseconds like there is in the full .Net Framework? If not you'll need to add all of the components of the timespan after you multiply them out to get the total.


Right. That must be it.. stupid mistake.

I was looking for the Stopwatch Class be couldn't vind it.. so wrote my own (including stupid mistakes)

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.