1

i wanted to test how much time is lost by putting code into methods instead of just calling it.

To my surprise, the Method-Calls are faster and i ask myself: why?

This is the code: The HObjects are part of the image processing library "Halcon" and are very commonly used with "out" in method calls, so dont worry about the horrible look ;-)

    static void Main(string[] args)
    {
        int runs = 900000;
        HObject hob1;
        HObject hob2;
        HObject hob3;
        HObject hob4;
        HObject hob5;
        HObject hob6;

        DateTime t1 = DateTime.Now;

        for ( int i = 0; i < runs; i++ )
        {
            doItMethod(out hob1, out hob2, out hob3, out hob4, out hob5, out hob6);
        }

        TimeSpan ts1 = TimeSpan.FromTicks(DateTime.Now.Ticks - t1.Ticks);

        DateTime t2 = DateTime.Now;

        for ( int i = 0; i < runs; i++ )
        {
            HOperatorSet.GenEmptyObj(out hob1);
            HOperatorSet.GenEmptyObj(out hob2);
            HOperatorSet.GenEmptyObj(out hob3);
            HOperatorSet.GenEmptyObj(out hob4);
            HOperatorSet.GenEmptyObj(out hob5);
            HOperatorSet.GenEmptyObj(out hob6);

            hob1.Dispose();
            hob2.Dispose();
            hob3.Dispose();
            hob4.Dispose();
            hob5.Dispose();
            hob6.Dispose();
        }

        TimeSpan ts2 = TimeSpan.FromTicks(DateTime.Now.Ticks - t2.Ticks);

        Console.WriteLine("Zeitspanne Methodenaufruf : " + ts1.TotalMilliseconds.ToString());
        Console.WriteLine("Zeitspanne direkter Aufruf: " + ts2.TotalMilliseconds.ToString());

        Console.ReadKey();
    }


    static void doItMethod(out HObject hobOut1, out HObject hobOut2, out HObject hobOut3, out HObject hobOut4, out HObject hobOut5, out HObject hobOut6)
    {
        HOperatorSet.GenEmptyObj(out hobOut1);
        HOperatorSet.GenEmptyObj(out hobOut2);
        HOperatorSet.GenEmptyObj(out hobOut3);
        HOperatorSet.GenEmptyObj(out hobOut4);
        HOperatorSet.GenEmptyObj(out hobOut5);
        HOperatorSet.GenEmptyObj(out hobOut6);

        hobOut1.Dispose();
        hobOut2.Dispose();
        hobOut3.Dispose();
        hobOut4.Dispose();
        hobOut5.Dispose();
        hobOut6.Dispose();
    }
4
  • 3
    I have a suspicion: It's a GC artifact. To verify it, please swap the calls: Test "direkter AUfruf" (direct call) first, THEN method call and repost. Commented Sep 12, 2012 at 9:05
  • 1
    Discover Stopwatch class (msdn.microsoft.com/en-us/library/…) for yourself. Is the difference considerable (post time values, please)? What build configuration you've tested (debug or release)? Commented Sep 12, 2012 at 9:05
  • C# compiler optimizes code, and it may be the reason, but you must try it with Stopwatch class Commented Sep 12, 2012 at 9:06
  • As others have pointed out, don't use DateTime for performance timing, use System.Diagnostics.StopWatch that's what it's for and it's much better at doing it. Also how much difference are you talking about? You need to provide actual numbers on what difference in performance you're seeing. Commented Sep 12, 2012 at 9:06

3 Answers 3

1

You should use Stopwatch

 //add using System.Diagnostics;

 public static void Main()
    {
        int runs = 900000;
        HObject hob1;
        HObject hob2;
        HObject hob3;
        HObject hob4;
        HObject hob5;
        HObject hob6;

        Stopwatch t1 = new Stopwatch();

        t1.Start();

        for ( int i = 0; i < runs; i++ )
        {
            doItMethod(out hob1, out hob2, out hob3, out hob4, out hob5, out hob6);
        }

        t1.Stop();


        Stopwatch t2 = new Stopwatch();
        t2.Start();

        for ( int i = 0; i < runs; i++ )
        {
            HOperatorSet.GenEmptyObj(out hob1);
            HOperatorSet.GenEmptyObj(out hob2);
            HOperatorSet.GenEmptyObj(out hob3);
            HOperatorSet.GenEmptyObj(out hob4);
            HOperatorSet.GenEmptyObj(out hob5);
            HOperatorSet.GenEmptyObj(out hob6);

            hob1.Dispose();
            hob2.Dispose();
            hob3.Dispose();
            hob4.Dispose();
            hob5.Dispose();
            hob6.Dispose();
        }
        t2.Stop();



        Console.WriteLine("Zeitspanne Methodenaufruf : " + t1.ElapsedMilliseconds);
        Console.WriteLine("Zeitspanne direkter Aufruf: " + t2.ElapsedMilliseconds);

        Console.ReadKey();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I got right results using Stopwatch:

Method: 00:00:04.5573170
Code: 00:00:04.5539918



static void Main(string[] args)
    {
        int runs = 1000;
        HObject hob1;
        HObject hob2;
        HObject hob3;
        HObject hob4;
        HObject hob5;
        HObject hob6;

        Stopwatch sw = new Stopwatch();
        sw.Start();

        for (int i = 0; i < runs; i++)
        {
            doItMethod(out hob1, out hob2, out hob3, out hob4, out hob5, out hob6);
        }

        sw.Stop();
        Console.WriteLine(sw.Elapsed);

        sw = Stopwatch.StartNew();

        for (int i = 0; i < runs; i++)
        {
            HOperatorSet.GenEmptyObj(out hob1);
            HOperatorSet.GenEmptyObj(out hob2);
            HOperatorSet.GenEmptyObj(out hob3);
            HOperatorSet.GenEmptyObj(out hob4);
            HOperatorSet.GenEmptyObj(out hob5);
            HOperatorSet.GenEmptyObj(out hob6);

            hob1.Dispose();
            hob2.Dispose();
            hob3.Dispose();
            hob4.Dispose();
            hob5.Dispose();
            hob6.Dispose();
        }

        sw.Stop();
        Console.WriteLine(sw.Elapsed);

        Console.ReadKey();
    }

1 Comment

You should edit your question instead of posting this as an answer. That way it's more clear to the people trying to help you.
0

Thanks people! I got my answer.

As Eugen Rieck suspected, it was a GC-Artifact.

Enforcing garbage collection makes the method-call version slower than direct code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.