• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert long to string  RSS feed

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello experts !

I wanted to ask you about the most efficient way to convert a long number to it's string representation.

E.g 1000000 to "1000000".

I know two ways : 1. the cast (String) longNum and 2. String str = " " + longNum.

Please advice.

Thank you for all your time and effort,

Kind regards,
Marius
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. the cast (String) longNum

This won't work. Another way you can consider is to convert to Long and calling its toString() method.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
1. the cast (String) longNum

This won't work. Another way you can consider is to convert to Long and calling its toString() method.


Thank you very much for your answer John !

so you're saying that toString() is the most efficient way ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:so you're saying that toString() is the most efficient way ?

I don't know. May be other ranchers can help.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want one of the many overloaded versions of this. It gives a suggestion for an alternative method, too.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need to find out the most efficient way? you have performance issue?
Only concern the performance when you have issue, readability is more important.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Tong wrote:Why would you need to find out the most efficient way? you have performance issue?
Only concern the performance when you have issue, readability is more important.
\

how could readability be more important than performance ???
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You probably want one of the many overloaded versions of this. It gives a suggestion for an alternative method, too.


Thank you very much Ritchie !

But is this the most efficient way to convert to string ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
Campbell Ritchie wrote:You probably want one of the many overloaded versions of this. It gives a suggestion for an alternative method, too.


Thank you very much Ritchie !

But is this the most efficient way to convert to string ?


You don't need to worry about that. Any method you're likely to come across will be about the same.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
Raymond Tong wrote:Why would you need to find out the most efficient way? you have performance issue?
Only concern the performance when you have issue, readability is more important.
\

how could readability be more important than performance ???

I have read few books talked about this but could not recall which one.
Here is one of the discussion
http://stackoverflow.com/questions/183201/should-a-developer-aim-for-readability-or-performance-first

I think the example is good, the method suppose to multiple the input by 2 and return.
It would be easier to read "x * 2" compared with "x << 1".

Also, Java do optimization for your code automatically (if it detects usual pattern), which may faster than manual tuning.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
Raymond Tong wrote:Why would you need to find out the most efficient way? you have performance issue?
Only concern the performance when you have issue, readability is more important.
\

how could readability be more important than performance ???


It's not that one is absolutely more important than the other. Rather, the issue is that many developers--especially beginners, but lots of experienced folks as well--spend too much time worrying about things that have no meaningful effect on performance. And they tend to ignore the fact that the cost of software maintenance is extremely high, and they don't make a realistic evaluation of that cost compared to the cost of, for example, faster hardware.

If I told you that code X is 100 times faster than code Y, would you say we should use code X?

If you answer yes, it's the wrong answer.
If you answer no, it's the wrong answer.
You don't have enough information.

Say, for example, the code in question validates an email address, and code X takes 1 microsecond per address and code Y takes 100 microseconds per address. In the context of reading the email address from user input or from a file or a database, validating the address, composing the email, and putting the email on the wire, that 1 microsecond vs. 100 microseconds is not going to be noticeable. If you send a million emails, it will make about a minute and a half difference.

When something goes wrong, and you're paying a developer, say, $40 / hour, how much computer time, at 1.5 minutes per million emails, does it take to make up for the cost of your developer fixing hard-to-read code vs. easy-to-read?

For an hour of your developer's time, I can buy 8GB of RAM. That might be enough to cut that 1.5 minutes down (not that it's even a meaningful difference to start with).

For a day or two of your developer's time, I can upgrade to the next fastest CPU/RAM/HD configuration that will not only wipe out that 1.5 minute difference here, but will make all my other apps run faster as well. Could your developer have made things more "efficient" across the board in two days?

The kind of performance issues you focus on when writing code are big-O stuff. Like using a HashMap when you want to find Person objects by name for O(1) performance rather than searching a List for them for O(N).

Stuff like the "efficiency" of converting a number to a String just doesn't make a difference, as long as you're using methods in the core API or a reputable 3rd party library. That kind of stuff comes up only after you've got all the right data structures and algorithms for the big stuff, and you've still got a bottleneck. And you'll only find it by measuring it, such as with a profiler. Humans are horrible at predicting where these kinds of bottlenecks will arise, and spending time coming up with "clever" ways to avoid them when you're first writing the code is wasteful and counterproductive.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:how could readability be more important than performance ???

Readability is very often more important than performance, especially if you're talking about micro-optimizations for performance.

A large part of the cost of software is in the maintenance of software. For most software, maintenance costs a lot more over the lifetime of the software product than the initial development. If you write unreadable code for the sake of performance, then that code will be confusing for other people who have to maintain the source code. They'll have to spend more time to learn and understand what the code does, and if it's complicated there's a higher chance that someone inadvertently breaks it, causing a bug.

On the other hand, hardware is cheap. Sometimes if code has a performance problem, the cheapest way to solve it might be to buy a faster machine.

With regard to micro-optimizations: Almost always it makes no sense to do micro-optimizations. Performance optimization should always be done by measuring the performance with a profiler to identify where the bottlenecks are, and then concentrate effort on fixing the bottlenecks. Optimizing single statements based only on a vague feeling often leads to nothing - because you're just fixing random little things, which might not be where the performance problem is in reality.

In real, commercial software development, there are much more factors that come into play than just writing "the most efficient" program.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote: . . . how could readability be more important than performance ???
Here, Brian Goetz explains why writing simple and readable code often allows better performance than trying to optimise code for oneself.

Also readability will make potential errors easier to find. Dangerous errors can lurk unnoticed if the code is difficult to read.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Marius Constantin wrote:
Raymond Tong wrote:Why would you need to find out the most efficient way? you have performance issue?
Only concern the performance when you have issue, readability is more important.
\

how could readability be more important than performance ???


It's not that one is absolutely more important than the other. Rather, the issue is that many developers--especially beginners, but lots of experienced folks as well--spend too much time worrying about things that have no meaningful effect on performance. And they tend to ignore the fact that the cost of software maintenance is extremely high, and they don't make a realistic evaluation of that cost compared to the cost of, for example, faster hardware.

If I told you that code X is 100 times faster than code Y, would you say we should use code X?

If you answer yes, it's the wrong answer.
If you answer no, it's the wrong answer.
You don't have enough information.

Say, for example, the code in question validates an email address, and code X takes 1 microsecond per address and code Y takes 100 microseconds per address. In the context of reading the email address from user input or from a file or a database, validating the address, composing the email, and putting the email on the wire, that 1 microsecond vs. 100 microseconds is not going to be noticeable. If you send a million emails, it will make about a minute and a half difference.

When something goes wrong, and you're paying a developer, say, $40 / hour, how much computer time, at 1.5 minutes per million emails, does it take to make up for the cost of your developer fixing hard-to-read code vs. easy-to-read?

For an hour of your developer's time, I can buy 8GB of RAM. That might be enough to cut that 1.5 minutes down (not that it's even a meaningful difference to start with).

For a day or two of your developer's time, I can upgrade to the next fastest CPU/RAM/HD configuration that will not only wipe out that 1.5 minute difference here, but will make all my other apps run faster as well. Could your developer have made things more "efficient" across the board in two days?

The kind of performance issues you focus on when writing code are big-O stuff. Like using a HashMap when you want to find Person objects by name for O(1) performance rather than searching a List for them for O(N).

Stuff like the "efficiency" of converting a number to a String just doesn't make a difference, as long as you're using methods in the core API or a reputable 3rd party library. That kind of stuff comes up only after you've got all the right data structures and algorithms for the big stuff, and you've still got a bottleneck. And you'll only find it by measuring it, such as with a profiler. Humans are horrible at predicting where these kinds of bottlenecks will arise, and spending time coming up with "clever" ways to avoid them when you're first writing the code is wasteful and counterproductive.


Thank you so so much Jeff for the many words of wisdom....
For a noob like me they are vital in becoming a great Java developer. Thank you so much for helping me get a bit better at what I do.

kind regards,
marius
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Marius Constantin wrote: . . . how could readability be more important than performance ???
Here, Brian Goetz explains why writing simple and readable code often allows better performance than trying to optimise code for oneself.

Also readability will make potential errors easier to find. Dangerous errors can lurk unnoticed if the code is difficult to read.


Thank you so much Ritchie for your time and for the link !
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I very much appreciate all the advice, I really wanted to ask what is the best practice in converting a long to it's string representation.

And another thing struck me regarding the code now, fix later approach.

If you don't pay attention to best practices, it would be like building a house without proper foundation, just so that you can move in faster, and when all the house is built, you come in and inspect the flaws.
And then fix, if possible, each one, finally ending up in rebuilding the house again, but because some flaws will be impossible to fix without causing more flaws, it will end up being nothing like a home to move in.

Wouldn't be easier to do as much as a great job as possible all the way, so you can fix as little as possible later ? and actually having a home to move in to.

thank you very much for your time, and advice.

kind regards,
marius
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding converting Long to String, I would suggest to use String.valueOf(). (read as getting string value and consistent as below)
Because for String converting to Long or other Number, it would be Long.valueOf() (benefit from caching)

Regarding best practice of whether to do things quick.
If you don't do something good or even correct, it would be hard to fix in the future (or higher cost).
Suppose you cook a dish and realize an ingredient was wrongly put, you may have to throw away and start from beginning.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Tong wrote:Regarding converting Long to String, . . .
It said long, not Long. For a Long, you would usually use its toString() method, but String.valueOf(Object o) will also work.
Raymond Tong wrote:Suppose you cook a dish and realize an ingredient was wrongly put, you may have to throw away and start from beginning.
In the case of cooking, that is wasteful. For some of the code you see posted on this forum, however, ctrl-A-delete is the fastest and most economical (both in terms of cost and effort) way to improve the code
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!