Jump to content

Delegates

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
zeelan

zeelan

    Newbie

  • Members
  • Pip
  • 1 posts
How delegates Can be Used In Realtime Programming

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Let me google that for you
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
That was a little vague...maybe this will be of some assistance: Delegates and Events in C# / .NET

#4
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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.)

#5
Liloi

Liloi

    Newbie

  • Members
  • Pip
  • 1 posts
Delegates is the links to methods. Used for a lot of things, for example for callback.

#6
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

Liloi said:

Delegates is the links to methods. Used for a lot of things, for example for callback.
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:
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
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

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:
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
Very imporant to know if you want your software to run on 20 year old computers. 18 nanoseconds in 0.000000018 seconds, right?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

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?
There's no place for sarcasm, I know what I'm talking about and I've done this many times in professional coding.

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...