G
gnewsgroup
I am not sure if this question makes sense. But I have been wondering
if there is a performance difference between a static method and an
instance method.
I created something like this, just to give it a test.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
Console.WriteLine(MyClass.GetString1());
DateTime end = DateTime.Now;
TimeSpan dur = end - start;
Console.WriteLine(dur.Milliseconds);
Console.WriteLine("#############");
start = DateTime.Now;
MyClass msc = new MyClass();
Console.WriteLine(msc.GetString2());
end = DateTime.Now;
dur = end - start;
Console.WriteLine(dur.Milliseconds);
}
}
class MyClass
{
public static string GetString1()
{
return "Just a junky string";
}
public string GetString2()
{
return "Just a junky string";
}
}
}
When I run it, I *always* a 0 duration for the instance method, but
for the static method, the duration is sometimes 0, sometimes 15 or
even larger.
I guess the task of the methods in the code above is too light to
yield any significant result. So, I am wondering, in general, if
there is a performance difference between a static method and an
instance method, assuming that they do exactly the same thing.
Anyone has an idea? Thank you.
if there is a performance difference between a static method and an
instance method.
I created something like this, just to give it a test.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
Console.WriteLine(MyClass.GetString1());
DateTime end = DateTime.Now;
TimeSpan dur = end - start;
Console.WriteLine(dur.Milliseconds);
Console.WriteLine("#############");
start = DateTime.Now;
MyClass msc = new MyClass();
Console.WriteLine(msc.GetString2());
end = DateTime.Now;
dur = end - start;
Console.WriteLine(dur.Milliseconds);
}
}
class MyClass
{
public static string GetString1()
{
return "Just a junky string";
}
public string GetString2()
{
return "Just a junky string";
}
}
}
When I run it, I *always* a 0 duration for the instance method, but
for the static method, the duration is sometimes 0, sometimes 15 or
even larger.
I guess the task of the methods in the code above is too light to
yield any significant result. So, I am wondering, in general, if
there is a performance difference between a static method and an
instance method, assuming that they do exactly the same thing.
Anyone has an idea? Thank you.