java字符串反转 最优方案

import java.util.Stack;


import com.google.zxing.common.StringUtils;


/*
 * FileName: StringReverse.java
 * Date:     2017年3月28日 上午10:23:21  
 */


/**
 * @date 2017年3月28日 上午10:23:21
 */
public class StringReverse {


    /**
     * @param args
     */
    public static void main(String[] args) {
        int num = 10000 * 10000;
        String str = "ABCDEF";
        long start = System.currentTimeMillis();
        long end = System.currentTimeMillis();
        for (int j = 0; j < 5; j++) {
            System.out.println("-------start 第" + j + "次尝试------------");
            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse1(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse1:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse2(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse2:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse3(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse3:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse4(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse4:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse5(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse5:" + (end - start));




            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse7(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse7:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse8(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse8:" + (end - start));


            start = System.currentTimeMillis();
            for (int i = 0; i < num; i++) {
                reverse9(str);
            }
            end = System.currentTimeMillis();
            System.out.println("reverse9:" + (end - start));


        }
    }


    public static String reverse1(String s) {
        StringBuilder result = new StringBuilder(s.length());
        for (int i = s.length() - 1; i >= 0; i--)
            result.append(s.charAt(i));
        return result.toString();
    }


    public static String reverse2(String s) {
        char[] str = s.toCharArray();
        int begin = 0;
        int end = s.length() - 1;
        while (begin < end) {
            str[begin] = (char) (str[begin] ^ str[end]);
            str[end] = (char) (str[begin] ^ str[end]);
            str[begin] = (char) (str[end] ^ str[begin]);
            begin++;
            end--;
        }
        return new String(str);
    }


    public static String reverse3(String s) {
        StringBuilder sb = new StringBuilder(s);
        return sb.reverse().toString();
    }


    public static String reverse4(String s) {
        char ch[] = s.toCharArray();
        int start = 0, end = ch.length - 1;
        char temp;
        while (start < end) {
            temp = ch[start];
            ch[start] = ch[end];
            ch[end] = temp;
            start++;
            end--;
        }
        // String s1 = String.copyValueOf(ch);
        return new String(ch);
    }


    public static String reverse5(String orig) {
        char[] s = orig.toCharArray();
        int n = s.length - 1;
        int halfLength = n / 2;
        for (int i = 0; i <= halfLength; i++) {
            char temp = s[i];
            s[i] = s[n - i];
            s[n - i] = temp;
        }
        return new String(s);
    }


    public static String reverse7(String s) {
        char[] str = s.toCharArray();
        Stack stack = new Stack();
        for (int i = 0; i < str.length; i++)
            stack.push(str[i]);


        String reversed = "";
        for (int i = 0; i < str.length; i++)
            reversed += stack.pop();


        return reversed;
    }
    
    public static String reverse8(String s) {
        int length = s.length();
        if (length <= 1)
            return s;
        String left = s.substring(0, length / 2);
        String right = s.substring(length / 2, length);
        return reverse8(right) + reverse8(left);
    }


    public static String reverse9(String s) {
        int length = s.length();
        String reverse = "";
        for (int i = 0; i < length; i++)
            reverse = s.charAt(i) + reverse;
        return reverse;
    }


}



测试结果:

-------start 第0次尝试------------
reverse1:4192
reverse2:2469
reverse3:3612
reverse4:1923
reverse5:1643
reverse7:33509
reverse8:31630
reverse9:15486
-------start 第1次尝试------------
reverse1:3989
reverse2:2376
reverse3:3710
reverse4:1901
reverse5:1767
reverse7:33324
reverse8:30506
reverse9:15462


由此可知,将string转换为char[],在char[]中进行字符反转效率最高。

注意:如果调用其他第三方包中,大多调用 new StringBuilder(s.reverse().toString();方式,

所以有时最好自己动手写点通用函数。


参考 http://blog.csdn.net/qq_27603235/article/details/50752809

http://www.cnblogs.com/Claire6649/p/6235916.html

https://www.oschina.net/code/snippet_12_9060

你可能感兴趣的:(java)