Download "user manual"
Transcript
Beginners guide to C# and .NET Micro Framework
Thinking Small
long ticks = DateTime.Now.Ticks;
for (int i = 0; i < 1000; i++)
WriteRegister(5, 100);
ticks = DateTime.Now.Ticks - ticks;
ms = ticks / TimeSpan.TicksPerMillisecond;
Debug.Print("Time = " + ms.ToString());
}
}
}
In the second example, the garbage collector had to run only twice and it took only 448
milliseconds, about half a second to run. We only moved one line of code and it is 4 times
faster.
Let us move the buffer globally and see.
using
using
using
using
System.Threading;
System;
Microsoft.SPOT.Hardware;
Microsoft.SPOT;
namespace MFConsoleApplication1
{
public class Program
{
static SPI _spi = new SPI(new SPI.Configuration(
Cpu.Pin.GPIO_NONE, false, 0, 0, false,
true, 1000, SPI.SPI_module.SPI1));
static byte[] buffer = new byte[2];
static void WriteRegister(byte register_num, byte value)
{
buffer[0] = register_num;
buffer[1] = value;
_spi.Write(buffer);
_spi.Dispose();
}
public static void Main()
{
long ms;
long ticks = DateTime.Now.Ticks;
for (int i = 0; i < 1000; i++)
WriteRegister(5, 100);
ticks = DateTime.Now.Ticks - ticks;
ms = ticks / TimeSpan.TicksPerMillisecond;
Debug.Print("Time = " + ms.ToString());
}
}
}
Copyright © 2010 GHI Electronics, LLC
Page 166/170