Calling a method on an object a bunch of times versus constructing an object a bunch of times

Posted by Ami on Stack Overflow See other posts from Stack Overflow or by Ami
Published on 2010-06-14T23:14:10Z Indexed on 2010/06/14 23:22 UTC
Read the original article Hit count: 150

I have a List called myData and I want to apply a particular method (someFunction) to every element in the List. Is calling a method through an object's constructor slower than calling the same method many times for one particular object instantiation?

In other words, is this:

for(int i = 0; i < myData.Count; i++)
    myClass someObject = new myClass(myData[i]);

slower than this:

myClass someObject = new myClass();
for(int i = 0; i < myData.Count; i++)
    someObject.someFunction(myData[i]);

?

If so, how much slower?

© Stack Overflow or respective owner

Related posts about c#

Related posts about objects