I did a test like below ↓
1) Create a customer enum (copy from the dayofweek)
[Serializable]
public enum Tester
{
// 概要:
// Indicates Sunday.
Sunday = 0,
//
// 概要:
// Indicates Monday.
Monday = 1,
//
// 概要:
// Indicates Tuesday.
Tuesday = 2,
//
// 概要:
// Indicates Wednesday.
Wednesday = 3,
//
// 概要:
// Indicates Thursday.
Thursday = 4,
//
// 概要:
// Indicates Friday.
Friday = 5,
//
// 概要:
// Indicates Saturday.
Saturday = 6,
}
2) Create two test method ...
static void TestEnumToString()
{
var t = Tester.Sunday;
Enumerable.Range(0, 1000000).ToList().ForEach(i => t.ToString());
}
static void DayOfWeekEnumToString()
{
var t = DayOfWeek.Sunday;
Enumerable.Range(0, 1000000).ToList().ForEach(i => t.ToString());
}
3) Main method
static void Main()
{
Stopwatch sw = new Stopwatch();
sw.Start();
TestEnumToString();
sw.Stop();
Console.WriteLine("Tester:" + sw.ElapsedMilliseconds);
sw = new Stopwatch();
sw.Start();
DayOfWeekEnumToString();
sw.Stop();
Console.WriteLine("DayOfWeek:" + sw.ElapsedMilliseconds);
Console.ReadKey();
}
4) The result :
Tester : 3164ms
DayOfWeek : 7032ms
I really don't know why the system type enum is slower than the customer enum type.... Could anybody tell me why ? Thank you...
UPDATE EDIT: Add the [ComVisible(true)] to the enum .
[ComVisible(true)]
[Serializable]
public enum Tester
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
}
The Result :
Tester : 5018ms
DayOfWeek : 7032ms
The system enum type still slower than the customer enum type...
Enum can be decorate with [ComVisible(true)] or [Flags] and each time it changes the result of your test .
[Serializable]
//[Flags]
[ComVisible(true)]
public enum Tester
{
// 概要:
// Indicates Sunday.
Sunday = 0,
You should add the [ComVisible(true)]
attribute if you want to compare apples to apples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With