| Author |
A substring question
|
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
Suppose you know the indexes i1, i2 of a string that define a substring, but you don't know which comes first
is there any other way besides the following
if( i1 < i2 ) {
return s.substring(i1,i2);
}
else return s.substring(i2,i1);
or
return s.substring( Math.min(i1,i2), Math.max(i1,i2) );
is one of these better that the other? Since it is an operation that conceivably can be performed many millions of times, CPU cycles is an issue. I could set up a test I suppose, but I'm curious what people have to say about it.
or return (i1<i2) ? s.substring(i1,i2) : s.substring(i2,i1);
thanks.
>
|
 |
Jeff Storey
Ranch Hand
Joined: Apr 07, 2007
Posts: 230
|
|
|
I would suggest writing a performance test for it if it's that big of a concern. Run it a million times with the different variations to get the timing. I suspect the first way (or maybe the last way with the ternary operator since it probably gets compiled to the same or very similar code) will be fastest. When trying s.substring(Math.min(i1,i2),Math.max(i1,i2)) you're calling Math.max when you don't have to since you know that the max is whichever value is not the min (unless they are the same, in which case it doesn't matter).
|
Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
|
 |
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
Jeff Storey wrote:I would suggest writing a performance test for it if it's that big of a concern. Run it a million times with the different variations to get the timing. I suspect the first way (or maybe the last way with the ternary operator since it probably gets compiled to the same or very similar code) will be fastest. When trying s.substring(Math.min(i1,i2),Math.max(i1,i2)) you're calling Math.max when you don't have to since you know that the max is whichever value is not the min (unless they are the same, in which case it doesn't matter).
Stands to reason, yes. I would have said pretty much the same thing if I had to choose.
This is for my chess program which I am tweaking, and I'm sure you can well imagine how the number nodes of a chess decision tree can grow incredibly large in a very short time. And the sort of operation I speak of can occur many times at any one node. Faster = Stronger. So it's a concern only insofar as very small incremental improvements become very significant when repeated millions of times.
Anyways, a millisecond timer on a method call is something I can and have done many times. I was just interested if the question can be definitively answered based on an understanding of what goes on at the machine level.
regards.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
I doubt that there is really a difference between the if-statement version and the tertiary operator version. Both load i1 and i2, compare them, then load s, i1 and i2 (the last two in the order as required) and call the substring method. The only difference after disassembling (javap -c) is that the if-statement version returns immediately if i1 < i2 whereas the tertiary operator version uses a jump to the single return statement. Technically speaking, that would make the if-statement one single jump faster.
The version with Math.min and Math.max will be slower. Not only does it use two extra method invocations, but you will compare i1 and i2 not just once but twice - one in Math.min and one in Math.max.
However, I doubt that you will notice differences between these 3 ways of more than a few nanoseconds. Don't optimize prematurely. When worrying about readability as well, I'd go for either the if-statement or the tertiary operator.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
Thanks Rob. yeah, if we are only talking a few nanoseconds, even 10 nanoseconds, then I would have to make some very significant improvements in other areas of my program, in order for the impact of this change to be noticeable.
still, it is worth knowing IMO. I agree that this particular microchange will be insignificant in and of itself. But when you consider that there may be many similar microchanges that can be made, then all of them together might start to make a difference.
regards.
|
 |
 |
|
|
subject: A substring question
|
|
|