Java 格式化字符串的最快、最有效的方法

Java 格式化字符串的最快、最有效的方法,java,string,string-matching,Java,String,String Matching,用Java将格式为20110913的字符串转换为2011-09-13的日期的最快方法是什么。使用StringBuilder,将插入“-”字符的子字符串附加在它们之间。使用StringBuilder,将插入“-”字符的子字符串附加在它们之间。使用Java.text.DateFormat: 使用java.text.DateFormat: 我做了一些简单的分析,发现了一些有趣的结果 public static String strcat(String ori){ return ori.subs

用Java将格式为20110913的字符串转换为2011-09-13的日期的最快方法是什么。

使用StringBuilder,将插入“-”字符的子字符串附加在它们之间。

使用StringBuilder,将插入“-”字符的子字符串附加在它们之间。

使用Java.text.DateFormat:

使用java.text.DateFormat:


我做了一些简单的分析,发现了一些有趣的结果

public static String strcat(String ori){
    return ori.substring(0, 4) + '-' + ori.substring(4, 6) + '-' + ori.substring(6);
}

public static String sdf(String ori){
    try {
        SimpleDateFormat in = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        Date temp = in.parse(ori);
        return out.format(temp);
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public static String sb(String ori){
    return new StringBuilder(10).append(ori.substring(0, 4)).append('-').append(ori.substring(4, 6)).append('-').append(ori.substring(6)).toString();
}

public static String nio(String ori){
    byte[] temp = ori.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(temp.length + 2);
    bb.put(temp, 0, 4);
    byte hyphen = '-';
    bb.put(hyphen);
    bb.put(temp, 4, 2);
    bb.put(hyphen);
    bb.put(temp, 6, 2);
    return new String(bb.array());
}

public static String qd(String ori){
    char[] result = new char[10];
    result[4] = result[7] = '-';

    char[] temp = ori.toCharArray();
    System.arraycopy(temp, 0, result, 0, 4);
    System.arraycopy(temp, 4, result, 5, 2);
    System.arraycopy(temp, 6, result, 8, 2);
    return new String(result);
}

public static void main(String[] args) {
    String ori = "20110913";
    int rounds = 10000;
    for (int i = 0; i < rounds; i++) {
        qd(ori);
        nio(ori);
        sb(ori);
        sdf(ori);
        strcat(ori);
    }
}
测试是使用JDK 7运行的。请注意,这不是一个广泛的评测,因为可以进行大量优化,例如缓存SimpleDataFormat、StringBuilder。此外,此测试不是多线程的。所以,一定要介绍你自己的程序!
p、 在美国,strcat比sb快不是我所期望的。我想编译器优化在这里起着很大的作用。

我做了一些简单的评测,发现了一些有趣的结果

public static String strcat(String ori){
    return ori.substring(0, 4) + '-' + ori.substring(4, 6) + '-' + ori.substring(6);
}

public static String sdf(String ori){
    try {
        SimpleDateFormat in = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        Date temp = in.parse(ori);
        return out.format(temp);
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public static String sb(String ori){
    return new StringBuilder(10).append(ori.substring(0, 4)).append('-').append(ori.substring(4, 6)).append('-').append(ori.substring(6)).toString();
}

public static String nio(String ori){
    byte[] temp = ori.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(temp.length + 2);
    bb.put(temp, 0, 4);
    byte hyphen = '-';
    bb.put(hyphen);
    bb.put(temp, 4, 2);
    bb.put(hyphen);
    bb.put(temp, 6, 2);
    return new String(bb.array());
}

public static String qd(String ori){
    char[] result = new char[10];
    result[4] = result[7] = '-';

    char[] temp = ori.toCharArray();
    System.arraycopy(temp, 0, result, 0, 4);
    System.arraycopy(temp, 4, result, 5, 2);
    System.arraycopy(temp, 6, result, 8, 2);
    return new String(result);
}

public static void main(String[] args) {
    String ori = "20110913";
    int rounds = 10000;
    for (int i = 0; i < rounds; i++) {
        qd(ori);
        nio(ori);
        sb(ori);
        sdf(ori);
        strcat(ori);
    }
}
测试是使用JDK 7运行的。请注意,这不是一个广泛的评测,因为可以进行大量优化,例如缓存SimpleDataFormat、StringBuilder。此外,此测试不是多线程的。所以,一定要介绍你自己的程序!
p、 在美国,strcat比sb快不是我所期望的。我想编译器优化在这里起到了很大的作用。

但不确定性能,我想做得更好。。。stringdate.substring0,4+'-'+stringdate.substring4,6+'-'+stringdate.substring6Maybe你应该在你的问题中添加一个字符串标记来表示字符串操作如果你找到满意的答案,你应该将你的问题标记为已回答。quick是相对的:-没有测量和上下文,这是一个非常无用的术语。顺便说一句,为什么这是标记秋千?但不确定的性能,我想得到比这更好的。。。stringdate.substring0,4+'-'+stringdate.substring4,6+'-'+stringdate.substring6Maybe你应该在你的问题中添加一个字符串标记来表示字符串操作如果你找到满意的答案,你应该将你的问题标记为已回答。quick是相对的:-没有测量和上下文,这是一个非常无用的术语。顺便说一句,为什么这是标记秋千?我以前是这样。。。stringdate.substring0,4+'-'+stringdate.substring4,6+'-'+stringdate.substring6顾名思义,作者想要快速的东西。使用dateformat很慢。使用@K-ballo方法大约快30倍。少量快30倍可能仍然不重要。我怀疑用户的瓶颈与格式有关。我以前是这样。。。stringdate.substring0,4+'-'+stringdate.substring4,6+'-'+stringdate.substring6顾名思义,作者想要快速的东西。使用dateformat很慢。使用@K-ballo方法大约快30倍。少量快30倍可能仍然不重要。我怀疑用户的瓶颈与格式化有关。strcat to sb:see strcat to sb:see
sb      15.7ms
strcat  15.2ms
qd      27.2ms
nio     137.6ms
sdf     582ms