Could be that my conversion is wrong. I just divided the ticks, I thought ticks is in ns…
But nevertheless this code is slower
public static void Main()
{
var led = new OutputPort(Generic.GetPin('B', 2), false);
Thread t = new Thread(() =>
{
while (true)
{
long d1 = DateTime.Now.Ticks;
for (int i = 0; i < 100000; i++)
{
led.Write(true);
led.Write(false);
}
long d2 = DateTime.Now.Ticks;
Debug.Print((d2 - d1).ToString());
}
});
//t.Priority = ThreadPriority.Highest;
t.Start();
//Thread.Sleep(500000);
}
than this one:
public static void Main()
{
var led = new OutputPort(Generic.GetPin('B', 2), false);
while (true)
{
long d1 = DateTime.Now.Ticks;
for (int i = 0; i < 100000; i++)
{
led.Write(true);
led.Write(false);
}
long d2 = DateTime.Now.Ticks;
Debug.Print((d2 - d1).ToString());
}
}
I always want to understand things. I would have expected that it makes no difference…
Yes, but Main thread is never waken up in this case. There’s no context switching, so the other thread should get 100% of CPU. I’m surprised the difference is so big.
@ Simon from Vilnius -[quote] Yes, there are two threads, but there should be no thread-switching — main thread sleeps forever…[/quote]
Each thread gets a 20ms time slice (I think, may not be the exact number); So, every 20 ms of interpreter time (note CPU); a thread is stopped (which probably entails a lot of data shuffling). How and/what it does to manage its thread queue is unknown to me; but, the time to check the execution state of the idle main may actually be significant… just a guess on my part.
One way that might be used to check would be to add threads that do nothing that spin doing the LEDs; add more threads and see how much the timing changes.
May be the difference is just because of VS debugger is attached while code running? Thus CLR needs to track states of the two threads in slower case and state only for one thread in faster case.
you are right. It is not that important to solve this.
But I’m very interested to understand the API. And if the API does something unexpected then it is a bug or “by design”…
Just wanted to find out what it is…