8

I'm interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I've already seen this so I'm not looking for a discussion about the difference between the two or a discussion of which is "better." I'm only interested in relative performance between the two. Thanks in advance.

-Mike

Community
  • 1
  • 1
MikeSchinkel
  • 4,947
  • 4
  • 38
  • 46
  • 1
    FYI I think a better general answer of why prefer Singleton rather than a static class could be forund here. http://stackoverflow.com/questions/6829609/php-singleton-vs-full-static-class-when-use-what/6829705#6829705 – Marco Demaio Aug 13 '11 at 10:27
  • 1
    [This post](http://stackoverflow.com/a/26592002/195835) on Stackoverflow gives some great benchmarks across different PHP versions and a good answer to the question. – Simon East May 01 '17 at 21:46

5 Answers5

22

Check this chart :)

alt text

grabbed from this article

Community
  • 1
  • 1
Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
  • 7
    Anyone can produce fancy charts. Without information about the used test setup, it is meaningless. You could at least provide a link to where you got this from. – Gordon Aug 05 '10 at 21:55
  • 4
    I just added the link to the article – Andreas Linden Aug 05 '10 at 23:04
  • 2
    As a side the author of the test should state loud and clear that he is calling in the loop everytime `TestSingleton::getInstance()` whilest in real world apps it happens that Singleton instances are passed as arguments to class methods (which is one of the advantage of the Singleton design vs the static class [http://stackoverflow.com/questions/6829609/php-singleton-vs-full-static-class-when-use-what/6829705#6829705]) thus those classes won't call everytime `getInstance()` because they already store a reference to the Singleton object... – Marco Demaio Aug 13 '11 at 10:59
  • 1
    ...anyway in real world apps it's also true that there are still many calls to Singletons `getInstance()` withour stroing any reference and if that is the case the test done could be a good simulation. It's hard to say! There are also tests that proves that a **`static` call is SLOWER than a method call** in PHP http://stackoverflow.com/questions/1472721 but also in that case it's hard say it depends also on what the app does http://stackoverflow.com/questions/1472721/php-performance-of-static-methods-vs-functions/7045679#7045679 – Marco Demaio Aug 13 '11 at 11:07
  • this test is just wrong, php profiling is not that simple. You have to measure CPU and memory consumption on server side in this case. In generale case you have also to track script duration and starting time of first output byte and time of last output byte.. There's also a GC you know, and depending on where GC happens it may alter timing. Measuring whole script execution may provide a more accurate estimate keeping into account GC.. Also sometimes is faster static, sometimes is faster singleton, sometimes is faster creating new instances!. – CoffeDeveloper Apr 16 '15 at 20:51
18

Unless you're calling them in a tight loop (meaning no other significant code, where the overhead of the call is significant) thousands or hundreds of thousands of times, don't worry about it. The difference is likely going to be under a microsecond, so it's not worth fretting over. Simply make the best architectural choice...

Premature optimization is the root of all evil...

Edit: To all the downvoters, here's a blog post that I wrote that describes why performance comparisons like this are all but useless.

ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • +1, totally agree, with the addition that when talking about wrapping resources which may or may not have been instantiated (socket streams, database connection etc.), the extra overhead of the `instance()` method of a Singleton is negated, as the static functions will still have to check whether a resource exists & create it if need be. – Wrikken Aug 05 '10 at 21:03
  • 4
    -1: Didn't answer the question, pontificated instead. The question is related to use within WordPress; should plugins use static classes to encapsulate hooks or instances of classes so yes it can be thousands of times or more. There's no need for instances in this context so I've chosen static but some are staying static methods are too slow. I didn't know what the facts were so I came here to find the answer. Still don't know the answer... – MikeSchinkel Aug 06 '10 at 21:09
  • 2
    I did answer the question. I said don't worry about it. The difference will likely be very minor. Make the better architectural choice. It's a micro-optimization either way, so don't worry about it... – ircmaxell Aug 07 '10 at 01:44
  • 2
    And it couldn't be an academic question? – Chris Tonkinson Jan 14 '11 at 04:32
  • 5
    @Mike using Wordpress and bothering about performance is a paradox. ircmaxell gave you the most sane advice there is. If you are really concerned about your code's performance, profile the application to find out where the bottlenecks are instead of just assuming they are where you think they are. An academic x is faster than y wont make your code run faster. – Gordon Aug 14 '11 at 14:20
  • @Gordon nice demonization of a platform that orders of magnitude more people use than any other... – MikeSchinkel May 02 '17 at 21:42
3

Before you can call the instance method of a singleton pattern object, you need to get the instance first, which requires a static method call:

SomeClass::getInstance()->myMethod();
// versus
SomeClass::myMethod();

So the first time you need access to that object in a function, you need to make a static method call first. Because function calls are never free, you are probably better off making the method static.

Ryan Tenney
  • 1,812
  • 3
  • 16
  • 29
1

In previous tests that I've done, I've found that calling static methods is faster than calling instance methods, and fractionally more memory efficient.... but the singleton shouldn't be dismissed purely for those reasons.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

I'm a bit late to this conversation, but having just found the question I figured I would toss my thoughts into the ring for my first post to SO.

As a quick experiment (after reading the article linked by zolex) I added a third test case to the article's benchmarks:

$inst = TestSingleton::getInstance();  
for($i=0;$i<$runs;$i++) $inst->test();

The results weren't 100% consistent of course, but I found that most times when running 500,000 calls through all three methods, the above method was running anywhere from 2-3 seconds faster than either of the other two.

Though I always cringe when I see the 'premature optimization' quote given, in this case I think it hits the nail on the head. The performance difference is minimal at best, and is usually in favor of the more sane singleton usage.

AndyM84
  • 139
  • 4
  • true, but read my comments in zolex answer. Your test simulates a real world apps where the singleton object is maybe passed as argument to other class methods (object injection) in thise cases the class store the reference to the Singleton object and does not need to call each time `getIntance`. But zolex's test STILL SIMULATES REAL WORLD APPS where singletons `getIntance` are called everywhere in the code without storing 1st a reference (it's not even an option when using different singletons classes). So both tests are right! But it's hard to say which one simulates better real world apps. – Marco Demaio Aug 13 '11 at 11:15
  • 1
    It'd probably be most accurate to say that real world applications will be a mixture of the two approaches for singleton usage. Either way, the real point I wanted to make was that singletons aren't as bad as they're made out. – AndyM84 Aug 23 '11 at 12:38