Performance ! which one is best

Hi guys could u explain which one is best in programming point of view

String s ="1 ";

double d = Double.parseDouble(s.trim());

String s ="1";

double d = Double.valueOf(s.trim()).doubleValue();

Can u explain the difference

Regards

Marimuthu.N

[409 byte] By [muthu2007a] at [2007-11-27 10:46:18]
# 1

Look at the source code to see what each method is doing.

camickra at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 2

both method should convert a string to double value

But i want which one is best

Regards

Marimuthu

muthu2007a at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 3

Both will execute in a fraction of a second. I doubt very much that worrying about which has a better performance is of any concern. If you write code, it works and doesn't take six hours to execute, then use it. When your code has an awful lag then you can start worrying about performance hits.

floundera at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks a lot

But one more question

Then why sun had provide these two methods?

Because i am asking interview point of view.

If suppose they asked the question like this then what i can tell

Could u please give suggestions

Regards

Marimuthu.N

muthu2007a at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 5

Tell them: "premature optimization is the root of all evil".

http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize

jbisha at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 6

Do you know how to execute a "bitch slap"? Most effective at an interview.

Hippolytea at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 7

If you get asked this in an interview, first look closely at whoever asked the question. If they're smirking slightly, it's a trick question and you should say so, before quoting "premature optimization" at them. If not, and it looks like a genuine question, make your excuses and leave, then go to the pub

georgemca at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 8

If you have the time to spend on worrying about optimization in a place in your code like THIS, you are doing just fine.

jGardnera at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 9

Without looking at the source code, I guess the first one will run slightly faster, since it just returns the primitive double value, where the second one first wraps it with its wrapper class and then unwraps it again.

But as the others said: this micro tuning which hardly is worth the effort!

-Puce

Pucea at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...
# 10

Thanks puce

you clear my doubts

it is 100% correct.

Regards

Marimuthu

muthu2007a at 2007-7-28 20:18:10 > top of Java-index,Java Essentials,Java Programming...