3

Performance-wise is there any difference on doing this?:

 public static Class StaticTestClass()
 {
   public static void Function(object param) => //Do stuff with "param"
 }

and this:

 public Class ConstructedTestClass()
 {
   private object classParam;
   public ConstructedTestClass(object param)
   {
    classParam = param;
   }
   public void Function() => //Do stuff with "classParam"
 }

I think that there wouldn't be any performance differece if done it one single time, but what If I have to do it many times, and call Function() many times?

Will having many instances of ConstructedTestClass have a memory impact?

And will calling Function withing StaticTestClass with the parameter have any performance impact?

PS: There are similar questions to this but I can't find one that adresses performance upon many calls.

EDIT: I did some tests and this are the results:

With 1000000000 iterations and Creating a ConstructedClass each iteration.

Static way: 72542ms

Constructed way: 83579ms

In this case the static way is faster, then I tried not creating a class each time Function() is called, this are the results: [100000000 samples]

Static way: 7203ms

Constructed way: 7259ms

In this case there's almost no difference so I guess I can do whatever I like the most since i wont be creating 1000000000 instances of the class.

4
  • You said many times .. But how many ? It is maybe not too many to make a difference. You want to compare the two approaches, just try them and use Stopwatch class to measure the time.
    – Zein Makki
    Aug 12, 2016 at 20:19
  • There are a number of similar questions that could easily be duplicates. But more importantly have you actually profiled the code to see if there's enough of a difference to matter? My guess is you haven't since if you had, you'd find the difference so small that it would be almost impossible to measure correctly. You're far better off designing proper applications than worrying about this level of micro-optimization.
    – David L
    Aug 12, 2016 at 20:19
  • Race your horses! :) ericlippert.com/2012/12/17/performance-rant
    – haku
    Aug 12, 2016 at 20:29
  • @null: pls post your measurement reults as answer... not in your question.
    – JanDotNet
    Aug 12, 2016 at 20:54

2 Answers 2

2

Technically yes, the static method will be slightly faster per call, because a static method doesn't have to check and see if the object it's attached to (because it's not) has been instantiated. This happens behind the scenes. (Technically there will be other slight overhead to set up the object etc.)

This is not a really good reason under most circumstances to choose one over the other though. They have different purposes. The a static method can't maintain state of internal variables like an object can etc.

In your case I would probably pick the static method. Based on the code you show, you don't have a real need to maintain a reference to the object you want to do something to. Perform a function on it, and be done with it.

With the other approach you have to create an object, then call the method. Furthermore the way it's set up, you have to instantiate a new object for each target object you have to perform the action on, because there is a reference stored in a private variable the method acts on. To me this would be more confusing from a readability perspective.

1

One difference is, that the generated objects have to be garbage collected. That overhead doesn't occur for the static call.

I tested it for 100000000 iterations:

  • static version takes ~0.7 seconds
  • non-static version (creating the instance one time and call the method n times) takes ~ 0.7 seconds.
  • non-static version (creating one instance per call) takes ~1.4 seconds.
4
  • Calling the function isn't affected by garbage collection. In fact, you could call the function on the same instance for the lifetime of the application. Aug 12, 2016 at 20:32
  • Not if the parameter changes because it is passed to the constructor.
    – JanDotNet
    Aug 12, 2016 at 20:35
  • I'm doing the testing myself I'll edit the question and post the results.
    – Pau C
    Aug 12, 2016 at 20:43
  • Fine, I'm looking forward to seeing your results :).
    – JanDotNet
    Aug 12, 2016 at 20:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.