string dotNetClass = ""; string dotNet = dotNetClass.GetType().Assembly.FullName;
typeof(string).Assembly.FullName
Object.GetType
public class TypeInfoBenchmark { [Benchmark] public Type WithTypeOf() { return typeof(string); } [Benchmark] public Type WithGetType() { return string.Empty.GetType(); } } BenchmarkRunner.Run<TypeInfoBenchmark>();
Method | Mean | StdDev | ------------ |---------- |---------- | WithTypeOf | 0.9272 ns | 0.0044 ns | WithGetType | 2.3251 ns | 0.0441 ns |
public static void Test1() { Stopwatch test = new Stopwatch(); test.Start(); typeof_string(); test.Stop(); Console.WriteLine(string.Format("typeof(string).Assembly.FullName -> {0}", test.ElapsedTicks)); test.Reset(); test.Start(); GetType_FullName(); test.Stop(); Console.WriteLine(string.Format("Object.GetType -> {0}", test.ElapsedTicks)); } private static string typeof_string() { return typeof(string).Assembly.FullName; } private static string GetType_FullName() { return String.Empty.GetType().Assembly.FullName; }
public static void Test1() { string s1 = typeof_string(); string s2 = GetType_FullName(); const int iterations = 1000000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { string s3 = typeof_string(); if (s3 != s1) throw new InvalidOperationException(); } sw.Stop(); Console.WriteLine("typeof(string).Assembly.FullName -> {0}", sw.ElapsedTicks); sw.Reset(); sw.Start(); for (int i = 0; i < iterations; i++) { string s3 = GetType_FullName(); if (s3 != s2) throw new InvalidOperationException(); } sw.Stop(); Console.WriteLine("Object.GetType -> {0}", sw.ElapsedTicks); } private static string typeof_string() { return typeof(string).Assembly.FullName; } private static string GetType_FullName() { return String.Empty.GetType().Assembly.FullName; }
typeof(string).Assembly.FullName -> 64904 Object.GetType -> 67988
typeof(string).Assembly.FullName -> 99869 Object.GetType -> 98907
typeof(string)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)