Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

toString() method call  RSS feed

 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Currently, if i have lots of Collections in my Code, in which Generics is not implemented.
Also the toString() method is being called so many times at various places.

If i could re-build my code by implementing Generics for my Collections like e.g. RESTRICTING
to only String values as <String> & Avoid the method call for toString() method as well as avoiding
casting to String like String str = (String)..etc.........

How much will it affect my performance.... OR ?


Thanks...
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all else is equal, and you call toString() fewer times, then your performance would obviously be improved. Whether it would be significantly improved, or even noticeably improved, is much less obvious.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok.... That would be really great....

Paul Clapham wrote:If all else is equal, and you call toString() fewer times, then your performance would obviously be improved. Whether it would be significantly improved, or even noticeably improved, is much less obvious.


And then i would be removing all the toString() method calls from the code.

But when i was checking for the same. I was confused with random results.

I had written some code and then i calculated the time it takes With and Without toString method.
But on every execution, the time it gives me is not the same......


 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, performance measurements typically vary randomly for various reasons. To obtain better results, take the action whose performance you're trying to measure, and put it in a loop. Repeat it many, many times. Eventually you will either see more stable results, or lose interest.

For the question you're trying to answer, it's very possible that there is no significant performance difference from removing the extra toString() calls. So if you can't measure a significant difference, it's probably because there isn't one. This is not unusual. It's easy to waste time worrying about the things you think might cause a performance problem. It's usually better to ignore such issues until you actually observe a performance problem, and confirm that the code you're looking at is really the cause.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Kanitkar wrote:
And then i would be removing all the toString() method calls from the code.


How do you remove the to string calls without changing what the code is doing? Or do you mean you changed from,

System.out.println(string.toString()) to System.out.println(string) -- this won't make any difference at all....


Implementing Generics should have no impact on the performance - the byte code is almost the same as without generics. Remember Generics is compile time not run time. Having said that, its a good idea to have all new code implement generics.

Casting ... I don't think not casting would make any improvements either.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your application do? If it involves any I/O (database, file system, etc), the time spent on collections and toString is negligible.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that String.toString() looks like



and the Hotspot VM is definitely smart enough to optimize that method away, when there's enough info to do so.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:System.out.println(string.toString()) to System.out.println(string) -- this won't make any difference at all....


No, i was not talking about that toString() context.

@ All who replied....

I've almost got it....

But i was mentioning the context where we want to get some values from Vector


Thanks.


 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that you're using a Vector makes no difference. You're not actually calling toString() on the Vector, but on the things inside the Vector. Which are already Strings, apparently, or your proposed cast to (String) would fail. Whether you call toString() here or cast to (String), is pretty much irrelevant. Both of those have such trivially inconsequential effect on performance, when called on a String, it really doesn't matter. Don't worry about performance issues where none exist.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!