.Net performance Strut vs Class
.Net performance Strut vs Class
In this article I am writing a simple test to compare the performance of ‘Struct’ and a ‘Class’. Yes, everyone knows thats a ‘Struct’ wins because it is a value type. No, Absolutely No. Use a ‘Struct’ wisely. It can be of huge benefit to performance if used properly. If it is not used properly, it can a bottleneck also. Lets see 2 simple examples which illustrate this.
The following code snippet is going to show the benefits of Struct, creating a value type, reading it and removing it on the stack is much easier than creating an object on the heap. So ‘Struct’ is faster.
using System;
namespace StructVsClass
{
public struct TempStruct
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}
public class TempClass
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
for (int i = 0; i < 5000000; i++)
{
TempStruct ts = new TempStruct();
ts.I = i;
}
DateTime endOfStruct = DateTime.Now;
for (int i = 0; i < 5000000; i++)
{
TempClass tc = new TempClass();
tc.I = i;
}
DateTime endOfClass = DateTime.Now;
Console.WriteLine(endOfStruct.Subtract(start).Ticks);
Console.WriteLine(endOfClass.Subtract(endOfStruct).Ticks);
}
}
}
When I ran this code, I got the following result:
After that I made small changes to the code and within the for loop, I casted the struct to and object and casted the object back to a struct. Now as you guessed, the structs for loop ran slower because an value type is crested on stack and when we casted it an object was created on heap also, so it lost all of its performance benefits.
Here are the results.
The lesson is: use struct but use it wisely.
You can download the code used for this simple test here.
Uncomment the commented lines for the second test program i.e with the casting stuff.
Related Posts:
Profiling your .Net Applications Part - 1
Types in .Net - Part 1
Type in .Net - Part 2
PS:
Thanks to ‘Bart Czernicki’ for his valuable comment and for pointing my spelling mistake. And man its all over the post. As he suggested I will try to put in more useful information in a future article. Once again thank you very much Mr. Bart Czernicki.
.Net performance Strut vs Class
Tags: .Net, .Net 3.5, C#, Code, Code Snippet


June 9th, 2008 at 4:30 am
I know this is a simple example, but you really should be using the StopWatch class to measure performance (it is optmized for these kind of performance scenarios).
Second, its NOT strut…its Struct. I know that not everyone is perfect and there are spelling mistakes; but you can’t be writing a technical article and spell the name of the .net type/object incorrectly MULTIPLE times.
Third, you may also want to provide information WHY its faster or .net memory graphs of what is going on or some info between value/reference types.
Just wanted to give you some constructive feed back.
June 9th, 2008 at 5:24 am
Hmm interesting but speed isn’t everything. It could be even more interesting to see how much memory struct vs class consumed, if you ex. created 5000000 of each kind. I bet struct would do better there too?
June 9th, 2008 at 8:15 am
Hey Kristoffer Trolle and Bart Czernicki, I will definitely do a test and profile the memory and post them here. Thank you for your valuable suggestions.