JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
<< PREV    [ 1 2 3 4 5 ]    NEXT >
Re: Foiled by a Div2-250! (response to post by dimkadimon) | Reply
Why if you do indexOf(String) then indexOf(char) is also available ?
Why using String for "out" if StringBuffer is a must here ?

Please never code like this ;-)
Re: Foiled by a Div2-250! (response to post by TAG) | Reply
Java doesn't have indexOf(char) ;).

But for some reason it has indexOf(int ch) instead.
Re: Foiled by a Div2-250! (response to post by TAG) | Reply
TAG,
you're kind of an ass. the Java code works and in fact so does using String instead of String buffer. BTW, incase you were implying that I don't write "REAL" code I do in fact have a job programming computers.
Re: Foiled by a Div2-250! (response to post by aussie) | Reply
But whenever I use indexOf(int) I pass in a char ;-)
Re: Meh? (response to post by tster123) | Reply
Was that Doxygen link meant to be funny? Of course I know about Doxygen and of course that's not what I meant. Just take a look at the things I've already listed.
Re: Meh? (response to post by rgrig) | Reply
"PS: In fact, I would prefer to use a smarter language that allows me to write at least the method contract comment in a way understood by tools. (for example Spec#, Java+JML, Eiffel)"

I don't see how Doxygen doesn't meet this requirement. I also have to argue that writing method contracts in a specific way really has no realation to how "smart" the language is. Just the tools you are using.
Re: Meh? (response to post by tster123) | Reply
Spec#, JML, and Eiffel are all languages that allow one to express method contracts. They have semantics that make it possible to write tools that check the contract at runtime or that do static verification using a theorem prover to ensure the contract is obeyed for all possible executions. Doxygen pretty prints.

edit: I renew my plea to take a look at them if you want to understand what I am talking about.
Re: Meh? (response to post by rgrig) | Reply
I thought you were just talking about tools that format method contracts. Sorry about that. It would be nice to always work with languages like that; but alas, such is not the way of the industry.
Re: Foiled by a Div2-250! (response to post by TAG) | Reply
Why be so negative? I was just showing an alternative approach to the problem, not trying to write perfectly structured code.

I didn't test this so I did indexOf(String) just to be certain.

I use "out" to construct and return the answer. If I was modifying the original String then I would use StringBuffer.
Re: Foiled by a Div2-250! (response to post by dimkadimon) | Reply
I think his point about using StringBuffer was because doing lots of String concatenations can be very slow and also creates more memory that needs to be garbage collected since you create a new string each time and throw away the old one. Using StringBuffer's append is faster and (if I remember correctly) it acts more like ArrayList in that it just doubles the size of the buffer whenever it runs out of space so it doesn't need to allocate and throw away memory as often.

He doesn't need to be so rude about it ;), but he's correct that using StringBuffer would probably be a better idea for something like that.
Re: Foiled by a Div2-250! (response to post by aussie) | Reply
OK, now I see. Thanks for the explanation. I thought StringBuffer was like Vector - hell slow. I guess I was wrong.
Re: Foiled by a Div2-250! (response to post by dimkadimon) | Reply
just FYI:

I ran some speed tests on that algorithm.
2.8 seconds using String and String.indexOf(string);
1.7 seconds using String and String.indexOf(char);
.8 seconds using StringBuffer and String.indexOf(char);

although if you really were worried about effeciency you would use the original solution posted before this one. This one looks through the string for every letter of the alphabet. Better to look through the whole string and grab each letter as you pass it.
Re: Foiled by a Div2-250! (response to post by dimkadimon) | Reply
If you want it to be faster (and don't need thread safety) you can try StringBuilder.
Re: Foiled by a Div2-250! (response to post by tster123) | Reply
http://forums.topcoder.com/?module=Message&messageID=552299
http://forums.topcoder.com/?module=Message&messageID=552732
http://forums.topcoder.com/?module=Message&messageID=517583
http://forums.topcoder.com/?module=Message&messageID=285100
Re: Foiled by a Div2-250! (response to post by slex) | Reply
I ran each algorithm like 20 times, threw away outliers and took the average. I also created only 2 objects the whole time. Could it be that the numbers were a fluke every time? yes. But it is EXTREMELY unlikely. With such simple code and such constant times (the time rarely deviated more than .1 seconds) I am going to make the statement that Stringbuffer and indexOf(char) are faster. However, like I said, it doesn't really matter because if you wanted a fast algorithm you wouldn't be using that one.
<< PREV    [ 1 2 3 4 5 ]    NEXT >

RSS