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();
}
Stopwatchclass (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)?DateTimefor performance timing, useSystem.Diagnostics.StopWatchthat'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.