CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

RE: Java - Backwards - Second Draft


[Replies] [Reply] [View by Thread] [Help]
[Back To SNEAK Development Forum]

Posted by Lt dopel On 2005-02-26 18:38:08
In Reply to RE: Java - Backwards - Second Draft Posted by Tr flamebalrog On 2005-02-26 02:15:52

Lt
Lt dopel


As far a running time both are exactly the same, O(n). You have 2 string, one of length "n", the other of length "m." The worst case run time would be the shortest of the two, which is this case would be at most n. If there is some jvm optimization that makes one faster than the other, it would be on the order of milliseconds.


On 2005-02-26 02:15:52, flamebalrog wrote
>On 2005-02-24 20:18:01, SAJChurchey wrote
>>

>>public static String Backwards(String str){

>>      if(str == null || str.trim().equals(""))

>>        return "";

>>      str = str.trim();

>>      StringBuffer newString = new StringBuffer(str);

>>      return newString.reverse().toString();

>>}

>>

>
>A not-null-and-not-empty-and-not-space-only String (one that will be reversed) is trimmed twice with your code (1st to test it, 2nd after the test). You could avoid it.
>
>Also, I think ' "literalString".equals(expression) ' is faster than ' expression.equals("literalString") '...can anyone confirm ?


Replies:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02252 seconds


About Us | Privacy Policy | Mission Statement | Help