• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1001
  • Last Modified:

Singleton Model Vs Classes with Static methods

Hello All,

In terms of performance, which is better model, when we decide to cretae Singleton class/class with all static methods

Best Regards
Sudhakar Chavali

0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 5
  • 3
  • 3
  • +3
8 Solutions
 
petmagdyCommented:
Static class methods is slightly better, but SingleTone is more flexible, also SingleTone without syncronized staff is not thread safe
0
 
petmagdyCommented:
>>Static class methods is slightly better

i mean better in terms of preformance
0
 
sudhakar_koundinyaAuthor Commented:
the reason to ask this is, when Itest some static classes and singletoon classes, I get almost 50-50 results
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petmagdyCommented:
well, the only additional step in SingleTone is creating the Single instance, no much difference really some thing like .000001 millsecond difference
0
 
RMaruszewskiCommented:
It's not. When you have the singleton, you first have to get its instance, and then call the method with regard of this object. And calling static (class) methods is always faster, than calling object (instance) methods.
Look here: http://www.protomatter.com/nate/java-optimization/yoda.html
0
 
MogalManicCommented:
The difference in speed is because when you call a static method, you don't have to push the instance 'this' pointer on the stack.  When ever you call a non-static method the java compiler will add code to push the instance pointer on the stack before passing parameters.

The performance difference is almost neglable (approxamatly .3 millisecond on a 200MHZ machine using JDK 1.1[later JDK's may be faster]):
Invoke obj static method                              216 ns
Invoke obj instance method                          558 ns
(according to  RMaruszewski's link)

You should be looking on how the class is going to be used.  Is it a pure utility class.  An example in the JDK is the Math class.  All of its methods are static because each method stands on its own and does not depend on other methods.  They are meant to be fast because they will be called in the lowest levels of many programs.

The advantage to using the singleton pattern, is that the class can have state information stored in the instance(watch for thread safety though).  Another advantage is for the maintainablity of the code.  It is easy to convert a singleton to a factory without forcing a refactoring of all of the classes that use the singleton.
0
 
RMaruszewskiCommented:
> The difference in speed is because when you call a static method, you don't have to push the instance 'this' pointer on the stack.

Yup, but that's not only it. In case of static methods you don't have any dynamic binding, so that the VM doesn't have to examine any "virtual call table" and check if the this method hasn't been overriden. I think it's the main reason why static methods are much faster.
0
 
MogalManicCommented:
>In case of static methods you don't have any dynamic binding, so that the VM doesn't have to examine any "virtual call table" and check if the >this method hasn't been overriden.

This also can be removed by declaring the method final.
0
 
RMaruszewskiCommented:
> This also can be removed by declaring the method final.

...what is also nicely visible in the benchmark I sent. :-)
0
 
MogalManicCommented:
Invoke obj static method                            216 ns
Invoke obj final instance method                 303 ns


So the difference between calling a static method and a final non-static method is only <.1 milliseconds
0
 
RMaruszewskiCommented:
Or - the static methods are called 1.5 times faster than final non-static methods.
0
 
Oliver_DornaufCommented:
Calling static methods is faster as calling a final declared method.

Use of a singelton is not faster or slower than a class with static methods. Becouse in both cases you need to create a object instance. You use a singelton object if you need accesson a non static fieled. (and if you like globals...)
0
 
RMaruszewskiCommented:
> in both cases you need to create a object instance.

Why? You don't have to create any object instance, if you use static methods.
And in case on singleton, you create object instance only once...
0
 
JakobACommented:
When calling a static method the compilere may even chose to inline the code instead of making a subroutine call.
0
 
sudhakar_koundinyaAuthor Commented:
>> When calling a static method the compilere may even chose to inline the code instead of making a subroutine call

I didn't get u. Can u elaborate plz

0
 
JakobACommented:
static int increaseBy4 ( int n ) {
     return  n+4;
} //endmethod

can be substituted directly into the code you write as
        nr = nr +4;                        // just add and assign
instead of the line
        nr = increaseBy4( nr );        // subroutinecall plus add and assign

javacompiler routinely do that when certain it is truly equivalent code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now