Wednesday, February 7, 2007

Structs vs. Classes

Obviously structs are supposedly a lot quicker than classes. However some time ago I heard rumours that in .NET 1.0 some error in the .net framework would cause them to be slower in some cases. Today for some reason I felt like making a small comparison in .NET 2.0.

So, I made a small console program with the following code:


public struct TestStruct
{
public int A;
public bool B;
//public string S;
}

public class TestClass
{
public int A;
public bool B;
//public string S;
public TestClass()
{
}
}

class Program
{
static void Main(string[] args)
{
int MaxIterations = 1000000;

DateTime t1 = DateTime.Now;
TestStruct[] ts = new TestStruct[MaxIterations];
for(int i=0;i<MaxIterations;i++)
{
ts[i] = new TestStruct();
ts[i].A = 42;
ts[i].B = true;
// ts[i].S = "Hey Joe";
}
DateTime t2 = DateTime.Now;
TestClass[] tc = new TestClass[MaxIterations];
for(int i=0;i<MaxIterations;i++)
{
tc[i] = new TestClass();
tc[i].A = 42;
tc[i].B = true;
//tc[i].S = "Hey Joe";
}
DateTime t3 = DateTime.Now;
TimeSpan ts1 = (TimeSpan) (t2 - t1);
TimeSpan ts2 = (TimeSpan) (t3 - t2);
Console.WriteLine("Struct Time: {0} ms", ts1.TotalMilliseconds);
Console.WriteLine("Class Time: {0} ms", ts2.TotalMilliseconds);
Console.ReadLine();

}
}

I ran it both with the struct/class containing a string, and without.
Here's the results I got from running it (in debug mode):

Structs/Classes without string: 20 ms / 270 ms (= classes are 13,5 times slower than structs)
Structs/Classes with string: 60 ms / 430 ms (= classes are 7 times slower than structs)

The reason I'm trying with and without strings is of course that strings are located on the heap, not on the stack as the other value-types, hence they are quite a bit slower.

It's naturally no surprise that structs are faster than classes - they should be - but it's nice to get an idea of exactly how big a difference there is.

2 comments:

c0rle0ne said...

Hello,

And what about Static Class ?

I have a entity that have 2 string constant and 2 methods.

I would like to know what is the best to use struct or static calss.

======= STRUCT ========

internal struct OpenXMLFileExtention
{
public const string feTemplate = "dotx";
public const string feDocument = "docx";
public bool isTemplate(string filename)
{
return (System.IO.Path.GetExtension(filename).ToLower() == feTemplate) ;
}
public bool isDocument(string filename)
{
return (System.IO.Path.GetExtension(filename).ToLower() == feDocument);
}
}

OR

==== STATIC CLASS =====

internal static class OpenXMLFileExtention
{
public const string feTemplate = "dotx";
public const string feDocument = "docx";
public static bool isTemplate(string filename)
{
return (System.IO.Path.GetExtension(filename).ToLower() == feTemplate) ;
}
public static bool isDocument(string filename)
{
return (System.IO.Path.GetExtension(filename).ToLower() == feDocument);
}
}

c0rle0ne said...

i thought about my question, i m sorry, it is stupid, i have to use a static class in this case, sorry again, and nice article and blog.

Regards

Albéric