|
1. the cast (String) longNum
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.
Marius Constantin wrote:so you're saying that toString() is the most efficient way ?
\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.
Campbell Ritchie wrote:You probably want one of the many overloaded versions of this. It gives a suggestion for an alternative method, too.
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 ?
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 ???
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 ???
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.Marius Constantin wrote: . . . how could readability be more important than performance ???
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.
Campbell Ritchie wrote:
Here, Brian Goetz explains why writing simple and readable code often allows better performance than trying to optimise code for oneself.Marius Constantin wrote: . . . how could readability be more important than performance ???
Also readability will make potential errors easier to find. Dangerous errors can lurk unnoticed if the code is difficult to read.
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:Regarding converting Long to String, . . .
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 codeRaymond Tong wrote:Suppose you cook a dish and realize an ingredient was wrongly put, you may have to throw away and start from beginning.
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|