performance hit for declaring attributes
- I understand that using reflection to process attributes can take a bit of time, but if I am not always using the attribute - is there much of a performance hit instantiating a class?
Given the small fictional snippet, if I am instantiating my class often, do I have a performance hit because I have attributes on some entities within that class? I know some might respond saying if I am that worried about it perhaps I should redesign... but I am just curious about what people think. It is static metadata about the class, so I would hope not.
public class SomeClassThatICreateALot
{
public enum MyEnum
{
[MySpecialAttribute("sample")]
Val1,
[MySpecialAttribute(42)]
Val2,
Val3,
}
// more code...
}
Answers
Nope, no performance impact whatsoever with unanalyzed attributes.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;namespace ConsoleApplication71
{
class Program
{
static void Main(string[] args)
{
int iterations = 10000000;for (int i = 0; i < 10; i++)
{
Console.WriteLine("Iteration" + (i + 1));Console.WriteLine("With attributes: " + Test(() => new SomeClassThatICreateALot2(), iterations));
Console.WriteLine("Without attributes: " + Test(() => new SomeClassThatICreateALot(), iterations));
}
}
static long Test(Action action, int iterations)
{
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
action();
long result = sw.ElapsedMilliseconds;
return result;
}
}public class SomeClassThatICreateALot
{
[MySpecialAttribute(42)]
public string Value { get; set; }[MySpecialAttribute(33)]
public string AnotherValue { get; set; }
// more code...
}public class SomeClassThatICreateALot2
{
public string Value { get; set; }public string AnotherValue { get; set; }
// more code...
}public class MySpecialAttributeAttribute : Attribute
{
public MySpecialAttributeAttribute(string sample)
{
}public MySpecialAttributeAttribute(int sample)
{}
}
}
Output:
Iteration1
With attributes: 435
Without attributes: 225
Iteration2
With attributes: 208
Without attributes: 242
Iteration3
With attributes: 267
Without attributes: 305
Iteration4
With attributes: 237
Without attributes: 222
Iteration5
With attributes: 228
Without attributes: 237
Iteration6
With attributes: 235
Without attributes: 239
Iteration7
With attributes: 224
Without attributes: 225
Iteration8
With attributes: 230
Without attributes: 252
Iteration9
With attributes: 196
Without attributes: 213
Iteration10
With attributes: 211
Without attributes: 213
Pretty much even across the board.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byJi.ZhouMSFT, ModeratorTuesday, November 10, 2009 2:12 AM
All Replies
Nope, no performance impact whatsoever with unanalyzed attributes.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;namespace ConsoleApplication71
{
class Program
{
static void Main(string[] args)
{
int iterations = 10000000;for (int i = 0; i < 10; i++)
{
Console.WriteLine("Iteration" + (i + 1));Console.WriteLine("With attributes: " + Test(() => new SomeClassThatICreateALot2(), iterations));
Console.WriteLine("Without attributes: " + Test(() => new SomeClassThatICreateALot(), iterations));
}
}
static long Test(Action action, int iterations)
{
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
action();
long result = sw.ElapsedMilliseconds;
return result;
}
}public class SomeClassThatICreateALot
{
[MySpecialAttribute(42)]
public string Value { get; set; }[MySpecialAttribute(33)]
public string AnotherValue { get; set; }
// more code...
}public class SomeClassThatICreateALot2
{
public string Value { get; set; }public string AnotherValue { get; set; }
// more code...
}public class MySpecialAttributeAttribute : Attribute
{
public MySpecialAttributeAttribute(string sample)
{
}public MySpecialAttributeAttribute(int sample)
{}
}
}
Output:
Iteration1
With attributes: 435
Without attributes: 225
Iteration2
With attributes: 208
Without attributes: 242
Iteration3
With attributes: 267
Without attributes: 305
Iteration4
With attributes: 237
Without attributes: 222
Iteration5
With attributes: 228
Without attributes: 237
Iteration6
With attributes: 235
Without attributes: 239
Iteration7
With attributes: 224
Without attributes: 225
Iteration8
With attributes: 230
Without attributes: 252
Iteration9
With attributes: 196
Without attributes: 213
Iteration10
With attributes: 211
Without attributes: 213
Pretty much even across the board.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byJi.ZhouMSFT, ModeratorTuesday, November 10, 2009 2:12 AM


