For a project I was wondering what’s the performance penalty of using a C# struct (containing only one field) over using a local variable directly; i.e.:
int myVar;
vs.
struct MyStruct {
int MyVar;
}
The result: There’s no (real) difference!
Here are some stats (“Release” build):
Running each test 50,000,000,000 times
Running 'UseLocalVar'...
Done in 61329.359 ms
Running 'UseStructField'...
Done in 61414.885 ms
Running 'UseStructProperty'...
Done in 121383.416 ms
The first two results are what I was talking about. The third result uses a property instead of a field in the struct. It’s two times slower.
Here’s the code for the benchmark:
using System;
using System.Diagnostics;
class Benchmark {
const long LOOPS = 50000000000;
static void Main(string[] args) {
Benchmark benchmark = new Benchmark();
Console.WriteLine("Running each test {0:0,0} times", LOOPS);
Console.WriteLine();
Console.WriteLine("Running 'UseLocalVar'...");
Stopwatch stopWatch = Stopwatch.StartNew();
int test = 0;
for (long x = 0; x < LOOPS; x++) {
test += benchmark.UseLocalVar((int)x);
}
TimeSpan elapsed = stopWatch.Elapsed;
Console.WriteLine("Done in {0:0.000} ms", elapsed.TotalMilliseconds);
Console.WriteLine("Running 'UseStructField'...");
stopWatch = Stopwatch.StartNew();
test = 0;
for (long x = 0; x < LOOPS; x++) {
test += benchmark.UseStructField((int)x);
}
elapsed = stopWatch.Elapsed;
Console.WriteLine("Done in {0:0.000} ms", elapsed.TotalMilliseconds);
Console.WriteLine("Running 'UseStructProperty'...");
stopWatch = Stopwatch.StartNew();
test = 0;
for (long x = 0; x < LOOPS; x++) {
test += benchmark.UseStructProperty((int)x);
}
elapsed = stopWatch.Elapsed;
Console.WriteLine("Done in {0:0.000} ms", elapsed.TotalMilliseconds);
}
int UseLocalVar(int val) {
int test = val;
test = test + val;
return test;
}
int UseStructField(int val) {
TestStructField test = new TestStructField(val);
test.Value = test.Value + val;
return test.Value;
}
int UseStructProperty(int val) {
TestStructProperty test = new TestStructProperty(val);
test.Value = test.Value + val;
return test.Value;
}
private struct TestStructField {
public int Value;
public TestStructField(int value) {
this.Value = value;
}
}
private struct TestStructProperty {
public int Value { get; set; }
public TestStructProperty(int value) : this() {
this.Value = value;
}
}
}