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

LVL 14
sudhakar_koundinyaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.