How delegates Can be Used In Realtime Programming
Delegates
Started by zeelan, Jun 09 2010 08:47 AM
7 replies to this topic
#1
Posted 09 June 2010 - 08:47 AM
|
|
|
#2
Posted 09 June 2010 - 09:51 AM
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#3
Posted 13 June 2010 - 06:50 PM
That was a little vague...maybe this will be of some assistance: Delegates and Events in C# / .NET
#4
Posted 08 July 2010 - 02:17 PM
Can you be more specific in this question? What problem are you solving?
I mean, what is the problem in using delegates in realtime programming in general? Unless you require so high a performance that delegates are ruled out, but then there is no positive answer to your question... (There is work around the problem, though.)
I mean, what is the problem in using delegates in realtime programming in general? Unless you require so high a performance that delegates are ruled out, but then there is no positive answer to your question... (There is work around the problem, though.)
#5
Posted 12 July 2010 - 08:27 PM
Delegates is the links to methods. Used for a lot of things, for example for callback.
#6
Posted 13 July 2010 - 01:29 AM
Liloi said:
Delegates is the links to methods. Used for a lot of things, for example for callback.
So when using delegates in real-time programming, one must ensure that all delegate objects are re-used instead of created each time when required.
Here's the test bench code which measures invocation time for method and delegate when single integer is passed as an argument:
using System;
namespace TestApp
{
class Program
{
delegate void MethodRef(int x);
static void Main(string[] args)
{
int count = 100000000;
MethodRef mr = new MethodRef(TestMethod);
DateTime t1 = DateTime.Now;
for (int i = 0; i < count; i++)
TestMethod(i);
DateTime t2 = DateTime.Now;
for (int i = 0; i < count; i++)
mr(i);
DateTime t3 = DateTime.Now;
for (int i = 0; i < count; i++)
{
mr = new MethodRef(TestMethod);
mr(i);
}
DateTime t4 = DateTime.Now;
int nsec1 = (int)(t2.Subtract(t1).TotalMilliseconds * 1000000 / count);
int nsec2 = (int)(t3.Subtract(t2).TotalMilliseconds * 1000000 / count);
int nsec3 = (int)(t4.Subtract(t3).TotalMilliseconds * 1000000 / count);
Console.WriteLine("Invoke {0} nsec/call", nsec1);
Console.WriteLine("Delegate {0} nsec/call", nsec2);
Console.WriteLine("Delegate+alloc. {0} nsec/call", nsec3);
}
static void TestMethod(int x)
{
}
}
}
And this is the output on my system:Invoke 3 nsec/call Delegate 5 nsec/call Delegate+alloc. 18 nsec/call
#7
Posted 13 July 2010 - 02:45 AM
zoranh said:
Yes, but delegate invocation has time overhead of roughly 50% compared to direct method invocation. And if delegate object is allocated each time it is invoked, then it is slower for an order of magnitude compared to normal method invocation. This must be taken into account in real-time programming.
So when using delegates in real-time programming, one must ensure that all delegate objects are re-used instead of created each time when required.
Here's the test bench code which measures invocation time for method and delegate when single integer is passed as an argument:
So when using delegates in real-time programming, one must ensure that all delegate objects are re-used instead of created each time when required.
Here's the test bench code which measures invocation time for method and delegate when single integer is passed as an argument:
using System;
namespace TestApp
{
class Program
{
delegate void MethodRef(int x);
static void Main(string[] args)
{
int count = 100000000;
MethodRef mr = new MethodRef(TestMethod);
DateTime t1 = DateTime.Now;
for (int i = 0; i < count; i++)
TestMethod(i);
DateTime t2 = DateTime.Now;
for (int i = 0; i < count; i++)
mr(i);
DateTime t3 = DateTime.Now;
for (int i = 0; i < count; i++)
{
mr = new MethodRef(TestMethod);
mr(i);
}
DateTime t4 = DateTime.Now;
int nsec1 = (int)(t2.Subtract(t1).TotalMilliseconds * 1000000 / count);
int nsec2 = (int)(t3.Subtract(t2).TotalMilliseconds * 1000000 / count);
int nsec3 = (int)(t4.Subtract(t3).TotalMilliseconds * 1000000 / count);
Console.WriteLine("Invoke {0} nsec/call", nsec1);
Console.WriteLine("Delegate {0} nsec/call", nsec2);
Console.WriteLine("Delegate+alloc. {0} nsec/call", nsec3);
}
static void TestMethod(int x)
{
}
}
}
And this is the output on my system:Invoke 3 nsec/call Delegate 5 nsec/call Delegate+alloc. 18 nsec/call
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#8
Posted 13 July 2010 - 03:21 AM
Davide said:
Very imporant to know if you want your software to run on 20 year old computers. 18 nanoseconds in 0.000000018 seconds, right?
If you add couple of strings passed to this method, then you get near 100 nsec per call (including garbage collection which comes once in every second). Now invoke that 100,000 times per second, which is real-time application, and there you go - 1% of CPU goes to doing nothing at all!
Now invoke this method a million times in a second, and then your code spends 10% of CPU just invoking empty methods. And don't think this is imagination - socketing server with 10,000 active clients is good example. I have coded components that have million calls per second, half a million log entries per second, gigabyte of data processed per second - that is real-time programming.
And if you use any optimization tools, you'll see that everything is measured in nanoseconds, and then again - your code runs slow when started. That's because nanosecond is not that small as one may think: when multiplied by millions it becomes large.
So it's nothing to laugh about when asking questions about real-time programming and delegates...


Sign In
Create Account

Back to top









