Is there a difference between the type of client enumeration and the type of system enumeration

I did a test as below ↓

1) Create a transfer to the client (copy from the daily sheet)

[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 methods ...

  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) The 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) Result:

 Tester : 3164ms DayOfWeek : 7032ms 

I really don't know why system type enumeration is slower than client enumeration type .... Can someone tell me why? Thanks...

CHANGE UPDATES: Add [ComVisible (true)] to the enumeration.

 [ComVisible(true)] [Serializable] public enum Tester { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, } 

Result:

 Tester : 5018ms DayOfWeek : 7032ms 

The system enumeration type is even slower than the client enumeration type ...

+6
source share
2 answers

Enum can be decorated 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, 
+1
source

You must add the [ComVisible(true)] attribute if you want to compare apples with apples.

+1
source

Source: https://habr.com/ru/post/893042/


All Articles