﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>That happens when you get the code even slightly wrong
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment21</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment21</guid><pubDate>Sun, 02 Mar 2008 00:12:02 GMT</pubDate></item><item><title>Adam Tybor commented on Creating objects - Perf implications</title><description>Interesting.... I get Security.VerificationException at run time.  I was working on a patch for MicroKernel DefaultActivator and I can't even get your simple code to run.  Is it a 3.5 thing?
  
  
Operation could destabilize the runtime.
  
	System.Security.VerificationException: Operation could destabilize the runtime.
  
	at CreateIntance(Int32 , String )
  
  
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment20</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment20</guid><pubDate>Sun, 02 Mar 2008 00:08:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>dominique ,
  
Not really relevant, because it is object creation we are testing.
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment19</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment19</guid><pubDate>Sat, 01 Mar 2008 15:00:36 GMT</pubDate></item><item><title>dominique Gratpain commented on Creating objects - Perf implications</title><description>What about if you use properties (with accessors get/set) instead of public variables (Num and Name) ?
  
  
Have you test the same with vb.net ?
  
  
I am curious about these two points.
  
  
Thanks
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment18</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment18</guid><pubDate>Sat, 01 Mar 2008 10:43:32 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>Mark,
  
  
Creating a new dyamic method: 00.0012487
  
Creating a single object using Activator.CreateInstance: 0.0000063033382
  
  
I wouldn't worry about it
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment17</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment17</guid><pubDate>Thu, 28 Feb 2008 15:48:51 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>Martin,
  
I haven't checked.
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment16</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment16</guid><pubDate>Thu, 28 Feb 2008 14:50:03 GMT</pubDate></item><item><title>Mark Seemann commented on Creating objects - Perf implications</title><description>Thank you for teaching me something new today :)
  
  
What's the overhead associated with creating the dynamic method?
  
  
When you create a million objects, it's probably neglegible, but what if you are only creating a few? Is there a break-even where, below that number of objects, one of the other methods is more efficient, but for a higher number, a dynamic method is the best performer?
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment15</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment15</guid><pubDate>Thu, 28 Feb 2008 12:03:43 GMT</pubDate></item><item><title>Roger Alsing commented on Creating objects - Perf implications</title><description>I've posted an alternative to these approaches using Linq expression trees:
  
  
http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment14</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment14</guid><pubDate>Thu, 28 Feb 2008 08:45:42 GMT</pubDate></item><item><title>Jon Skeet commented on Creating objects - Perf implications</title><description>One interesting twist would to be running the same tests, but passing in a constant string. You're not creating 1 million objects here - you're creating *at least* 2 million.
  
  
I haven't run all of your tests, but just the "call the constructor" one: and on my laptop, creating 100 million instances goes down from taking 28.6 seconds to 0.97 seconds when you pass in "x" as the string parameter. Turning off method inlining pushes it up to 1.46 seconds.
  
  
In other words, at first sight (and I'm aware that benchmarking is a horribly fiddly business) the actual object creation is only taking about 5% of the time. The rest is the constant hit of converting an integer into a string.
  
  
Given that information, I think it's worth rerunning all the tests. Suddenly all the overheads of delegates will look more significant, and Activator.CreateInstance will be an absolute dinosaur - I suspect.
  
  
None of this detracts from your point that creating objects is indeed very cheap.
  
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment13</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment13</guid><pubDate>Thu, 28 Feb 2008 08:32:08 GMT</pubDate></item><item><title>Martin commented on Creating objects - Perf implications</title><description>Do you have figures when creating transient objects with IoC.Resolve? Just curious..
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment12</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment12</guid><pubDate>Thu, 28 Feb 2008 07:31:29 GMT</pubDate></item><item><title>Tor Langlo commented on Creating objects - Perf implications</title><description>Just curious, how long does the "i.ToString()" call take in this test?
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment11</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment11</guid><pubDate>Thu, 28 Feb 2008 05:17:18 GMT</pubDate></item><item><title>James Arendt commented on Creating objects - Perf implications</title><description>Another option is available in .NET 3.5. Build a lambda expression tree and compile it into a delegate. No need to write Emit statements as in the dynamic IL approach.
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment10</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment10</guid><pubDate>Thu, 28 Feb 2008 01:12:16 GMT</pubDate></item><item><title>Bill Barry commented on Creating objects - Perf implications</title><description>umm, err, ignore that last comment; that is basically what you did (I was thinking generic referred to .NET Generics).
  
  
I feel dumb sometimes.
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment9</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment9</guid><pubDate>Wed, 27 Feb 2008 22:00:25 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>There is
  
generator.Emit(OpCodes.Ldarg, 5)
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment8</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment8</guid><pubDate>Wed, 27 Feb 2008 21:59:36 GMT</pubDate></item><item><title>Bill Barry commented on Creating objects - Perf implications</title><description>I meant with the opcodes:
  
  
ILGenerator gen = method.GetILGenerator();
  
gen.Emit(OpCodes.Ldarg_0);// i
  
gen.Emit(OpCodes.Ldarg_1);// s
  
gen.Emit(OpCodes.Newobj, ctor);// new Created
  
  
there is:
  
OpCodes.Ldarg_0
  
OpCodes.Ldarg_1
  
OpCodes.Ldarg_2
  
OpCodes.Ldarg_3
  
  
but there isn't an OpCodes.Ldarg_4. How would you load one with more arguments? I figure those are shortcuts the compiler could use, but you should be able to generate a function which returns a "create instance" delegate with more arguments. I was trying to see if I could write a dynamic method with this signature:
  
delegate Created CreateCtor(params object[] s);
  
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment7</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment7</guid><pubDate>Wed, 27 Feb 2008 21:50:24 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>Bill
  
Um, the same way?
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment6</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment6</guid><pubDate>Wed, 27 Feb 2008 21:25:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>Chad,
  
I refuse to care about that kind of number. :-)
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment5</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment5</guid><pubDate>Wed, 27 Feb 2008 21:23:42 GMT</pubDate></item><item><title>Bill Barry commented on Creating objects - Perf implications</title><description>How would you generate a method to call a constructor which has more than 3 arguments?
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment4</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment4</guid><pubDate>Wed, 27 Feb 2008 20:37:16 GMT</pubDate></item><item><title>Chad Myers commented on Creating objects - Perf implications</title><description>Ayende: Oops, I was talkin' about the diff between the Dynamic Method and calling 'new' directly.
  
  
The diff/instance of those is 0.0000001370171 seconds 
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment3</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment3</guid><pubDate>Wed, 27 Feb 2008 19:59:24 GMT</pubDate></item><item><title>Ayende Rahien commented on Creating objects - Perf implications</title><description>Which 0.00007 seconds are you talking about? That is the cost of creating an object using Activator.CreateInstance
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment2</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment2</guid><pubDate>Wed, 27 Feb 2008 16:13:58 GMT</pubDate></item><item><title>Chad Myers commented on Creating objects - Perf implications</title><description>The extra 0.000007 seconds is probably in the call of the delegate, and not in the instance create.
  
  
What if you called the delegate directly from the loop instead of calling a func that calls another func?
</description><link>http://ayende.com/3167/creating-objects-perf-implications#comment1</link><guid>http://ayende.com/3167/creating-objects-perf-implications#comment1</guid><pubDate>Wed, 27 Feb 2008 16:11:30 GMT</pubDate></item></channel></rss>