0

I am writing 3 methods which can print an input string vertically (one line). But I am unaware of the best method since benchmarking with System.nanoTime() considerably changes with large variations, I mean if I calculate the time difference between start and end of the process. Please suggest which is best or better, opine what you think according to your knowledge.

Code snippet #1

/**
 * Prints all the characters in the argument string vertically using loop
 * @param str
 */
public static void toVertStringLoop(String str){

    if(str != null && !"".equals(str)){
        int strLen = str.length();

        for (int i=0; i<strLen; i++){
            System.out.println(str.charAt(i));
        }

    }

}

Code snippet #2

/**
 * Prints all the characters in the argument string vertically using recursion
 * @param str
 */
public static void toVertStringRecur(String str){

    if (str != null && !"".equals(str)){

        System.out.println(str.charAt(0)); //print the first character
        toVertStringRecur(str.substring(1, str.length())); //recursively call removing the first character
    }
}

Code snippet #3

/**
 * Prints all the characters in the argument string vertically using for-each loop
 * @param str
 */
public static void toVerticalStringIterator(String str){

    //for-each loop needs array or Iterator instance
    for(char ch : str.toCharArray()){
        System.out.println(ch);
    }

}
Robert
  • 10,403
  • 14
  • 67
  • 117
xploreraj
  • 3,792
  • 12
  • 33
  • 51
  • The first. 2) recursion overhead 3) memory allocation for char array overhead. 3 could be faster assuming the string would not have linear access time (eg. like a linked list), but there are iterators available for that. – Niklas R Mar 28 '14 at 11:44
  • 1
    You already got all the code. Benchmark it and tell us!! I would guess the first one probably comes out on top. You could also try buffering it and only making one System.out call. – anonymous Mar 28 '14 at 11:44
  • 1
    Using recursion is best way of doing process of single data, when comparing process time between other methods. – Sathesh Mar 28 '14 at 11:45
  • 1
    this question would be better suited on http://codereview.stackexchange.com/ – Sionnach733 Mar 28 '14 at 11:46
  • when benchmarking try running it e.g. 100000 times and meassure this. – Scheintod Mar 28 '14 at 11:47
  • 1
    System.out.println will by far take the most time. For benchmarking better use a StringBuilder and output it's content once. – Scheintod Mar 28 '14 at 11:48
  • 2
    sidenote: if (str != null && !"".equals(str)) can be shorter written as if( !"".equals(str) ) because equals checks for null. – Scheintod Mar 28 '14 at 11:49
  • This question belongs on [codereview.se] – Keppil Mar 28 '14 at 11:50
  • Sounds like your problem is not understanding how to benchmark in Java. Have a look at http://openjdk.java.net/projects/code-tools/jmh/, for an example framework, or search the Internet for advice on how to perform accurate benchmarking in Java. – Duncan Jones Mar 28 '14 at 11:51

1 Answers1

0

Code snippet #3 is better one.Since Code snippet #2 includes recursive method calls where JVM has to maintain a stack (which reduces performance and increases extra over head to JVM) and where in Code snippet #1 has same effect of Code snippet #3 but slightly deffer in performance because of for-each loop(Read : Is there a performance difference between a for loop and a for-each loop?)

Community
  • 1
  • 1
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24