Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Alternative to Activator.CreateInstance

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
26 Jan 2012CPOL 18.8K   3   5
Using expressions, you can achieve a faster result with less code.public static T New(){ Type t = typeof(T); Func method = Expression.Lambda>(Expression.Block(t, new Expression[] { Expression.New(t) })).Compile(); return method();}Furthermore, this can...
Using expressions, you can achieve a faster result with less code.

C#
public static T New<T>()
{
    Type t = typeof(T);
    Func<T> method = Expression.Lambda<Func<T>>(Expression.Block(t, new Expression[] { Expression.New(t) })).Compile();

    return method();
}


Furthermore, this can be refined further by capturing the method into a dictionary and saving it off for future use. As an example, you can see this tip[^], which does something like this.

From my own testing of 1000 iterations of both, mine (without caching) was 148 milliseconds, while your original was 220 milliseconds.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralReason for my vote of 1 No DM or Expressions are needed for ... Pin
nonexisto26-Jan-12 2:15
nonexisto26-Jan-12 2:15 
GeneralRe: Corrected. I removed the type constraint. I didn't realize... Pin
Andrew Rissing26-Jan-12 3:39
Andrew Rissing26-Jan-12 3:39 
GeneralI'd have to agree with your comment. The big bottleneck slow... Pin
Dean Oliver25-Jan-12 18:40
Dean Oliver25-Jan-12 18:40 
GeneralI am aware of this alternative. However my results where wel... Pin
Dean Oliver25-Jan-12 8:44
Dean Oliver25-Jan-12 8:44 
GeneralRe: The overhead in your method may likely be due to the dynamic... Pin
Andrew Rissing25-Jan-12 9:36
Andrew Rissing25-Jan-12 9:36 
The overhead in your method may likely be due to the dynamic keyword usage.

But either way, here is the code I used to test it:

-----------------

var sw = Stopwatch.StartNew();

for (int i = 0; i < 1000; ++i)
{
List<string> x = DynamicInitializer.New<List<string>>();
}

sw.Stop();
Console.WriteLine("New<t>: " + sw.ElapsedMilliseconds);

var di = new DynamicInitializer();
sw = Stopwatch.StartNew();

for (int i = 0; i < 1000; ++i)
{
List<string> y = di.NewInstance<List<string>>();
}

sw.Stop();
Console.WriteLine("NewInstance<t>: " + sw.ElapsedMilliseconds);

Console.ReadLine();

-----------------

Testing it again, it seems the values are much closer now - ~150ms vs ~220ms. But using Expressions, still wins out. It roughly equates to being 20% faster than the original code. It doesn't seem to vary much with different types or release/debug builds, so I'd be curious if you find where the difference you're speaking of shows up.

If I take your code and rewrite as follows:

public V NewInstance<V>() where V : class
{
var type = typeof(V);
var target = type.GetConstructor(Type.EmptyTypes);
var dynamic = new DynamicMethod(string.Empty,
type,
new Type[0],
target.DeclaringType);
var il = dynamic.GetILGenerator();
il.DeclareLocal(target.DeclaringType);
il.Emit(OpCodes.Newobj, target);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ret);

var method = (Func<V>)dynamic.CreateDelegate(typeof(Func<V>));
return method();
}

It improves the timing from ~220ms to ~190ms.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.