Skip to content

Instantly share code, notes, and snippets.

@dadhi
Forked from hypeartist/code.cs
Created July 16, 2022 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadhi/83b6611c5302385beaa54f424630fd4d to your computer and use it in GitHub Desktop.
Save dadhi/83b6611c5302385beaa54f424630fd4d to your computer and use it in GitHub Desktop.
Type instantiation (.ctor with parameters)
object obj;
var r = 0;
const int cnt = 1000000;
var constructorInfo = typeof(Class).GetConstructor(new[] { typeof(int) })!;
var hCtor = constructorInfo.MethodHandle;
RuntimeHelpers.PrepareMethod(RuntimeMethodHandle.FromIntPtr(hCtor.Value));
var pCtor = (delegate* managed<object, int, void>)hCtor.GetFunctionPointer();
var t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
obj = RuntimeHelpers.GetUninitializedObject(typeof(Class));
pCtor(obj, i);
var inst = Unsafe.As<object, Class>(ref obj);
r += inst._f;
}
var t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"RuntimeHelpers.GetUninitializedObject + .ctor invocation via function pointer:\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
obj = constructorInfo.Invoke(new object?[] { i });
var inst = Unsafe.As<object, Class>(ref obj);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"ConstructorInfo.Invoke:\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
var c = Activator.CreateInstance(typeof(Class), i);
var inst = Unsafe.As<object, Class>(ref c!);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"Activator.CreateInstance:\t\t\t\t\t\t\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
var inst = new Class(i);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"Implicit call via 'new':\t\t\t\t\t\t\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
Console.WriteLine();
Console.WriteLine($"Dummy output = {r}");
@dadhi
Copy link
Author

dadhi commented Jul 16, 2022

The class

class Class {
     private readonly int _f;
     public Class(){
          _f = -1;
     }
     public Class(int v){
          _f = v;
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment