在开发程序的时候,经常使用StringBuffer来进行字符串的拼接。如果在循环中来反复的做字符串拼接时,会清空Stringbuffer中的内容,然后再拼接新的字符串信息。
//有三种清空方式 StringBuffer buffer = new StringBuffer("我要被清空了 >_<"); buffer.setLength(0); buffer = new StringBuffer(); buffer.delete(0, buffer.length());
System.nanoTime() 返回最准确的可用系统计时器的当前值,以毫微秒为单位。(毫微妙就是纳秒)
System.currentTimeMillis() 返回以毫秒为单位的当前时间。
加入一段连接,说慎用currentTimeMillis() http://blog.sina.com.cn/s/blog_6b8bd9d80101fe8t.html 还是一脸懵逼
顺便记一下时间的换算关系
1秒=1000毫秒(ms)
1秒=1,000,000 微秒(μs)
1秒=1,000,000,000 纳秒(ns)
1秒=1,000,000,000,000 皮秒(ps)
后面还有飞秒 阿秒 等等一脸懵逼。。。。
从大到小都是1000倍的关系
晒一下测试代码
有的帖子说setLength(0)是效率最高的,结果测试出来就傻了
package test; public class testStringBufferClear { public static void main(String[] args) { for (int i = 0; i < 20; i++) { test(); } } private static void test() { StringBuffer buffer = new StringBuffer("我要被清空了 >_<"); long s1, s2; //方式一 s1 = System.nanoTime(); for (int i = 0; i < 5000000; i++) { buffer.setLength(0); } s2 = System.nanoTime(); System.out.println((s2 - s1)+"》》setLength(0)"); //方式二 s1 = System.nanoTime(); for (int i = 0; i < 5000000; i++) { buffer = new StringBuffer(); } s2 = System.nanoTime(); System.out.println((s2 - s1)+"》》new StringBuffer()"); //方式三 s1 = System.nanoTime(); for (int i = 0; i < 5000000; i++) { buffer.delete(0, buffer.length()); } s2 = System.nanoTime(); System.out.println((s2 - s1)+"》》delete(0, buffer.length())"); } }
结果变成了这个
81713251》》setLength(0) 69181455》》new StringBuffer() 76279905》》delete(0, buffer.length()) 81278911》》setLength(0) 56117204》》new StringBuffer() 410》》delete(0, buffer.length()) 65276917》》setLength(0) 50844375》》new StringBuffer() 0》》delete(0, buffer.length()) 69770153》》setLength(0) 60818167》》new StringBuffer() 0》》delete(0, buffer.length()) 69423257》》setLength(0) 32047503》》new StringBuffer() 0》》delete(0, buffer.length()) 64818358》》setLength(0) 73453416》》new StringBuffer() 411》》delete(0, buffer.length()) 69963922》》setLength(0) 64979285》》new StringBuffer() 0》》delete(0, buffer.length()) 71185656》》setLength(0) 34512727》》new StringBuffer() 0》》delete(0, buffer.length()) 66288871》》setLength(0) 32957234》》new StringBuffer() 0》》delete(0, buffer.length()) 69014781》》setLength(0) 47149618》》new StringBuffer() 0》》delete(0, buffer.length()) 65783510》》setLength(0) 122120758》》new StringBuffer() 0》》delete(0, buffer.length()) 67758563》》setLength(0) 29492784》》new StringBuffer() 0》》delete(0, buffer.length()) 64075711》》setLength(0) 30184524》》new StringBuffer() 0》》delete(0, buffer.length()) 63680783》》setLength(0) 30880371》》new StringBuffer() 410》》delete(0, buffer.length()) 66420240》》setLength(0) 29210340》》new StringBuffer() 0》》delete(0, buffer.length()) 63746878》》setLength(0) 30056850》》new StringBuffer() 0》》delete(0, buffer.length()) 71138445》》setLength(0) 44319434》》new StringBuffer() 0》》delete(0, buffer.length()) 63863878》》setLength(0) 30636106》》new StringBuffer() 0》》delete(0, buffer.length()) 65043737》》setLength(0) 29686963》》new StringBuffer() 0》》delete(0, buffer.length()) 67308624》》setLength(0) 32259336》》new StringBuffer() 0》》delete(0, buffer.length())
下面是单个测试结果
//方式一 80868383》》setLength(0) 75025740》》setLength(0) 67061896》》setLength(0) 64939053》》setLength(0) 69289834》》setLength(0) 64819179》》setLength(0) 64577377》》setLength(0) 76561527》》setLength(0) 69773027》》setLength(0) 65813479》》setLength(0) 64638135》》setLength(0) 64610220》》setLength(0) 65000222》》setLength(0) 63363444》》setLength(0) 65187012》》setLength(0) 64927969》》setLength(0) 65362308》》setLength(0) 64333933》》setLength(0) 65160738》》setLength(0) 65306476》》setLength(0) //方式二 66744558》》new StringBuffer() 67418235》》new StringBuffer() 60270932》》new StringBuffer() 61198727》》new StringBuffer() 31622195》》new StringBuffer() 81092121》》new StringBuffer() 48929260》》new StringBuffer() 31173898》》new StringBuffer() 31100413》》new StringBuffer() 50957271》》new StringBuffer() 119894872》》new StringBuffer() 35147405》》new StringBuffer() 35889229》》new StringBuffer() 34537358》》new StringBuffer() 35683144》》new StringBuffer() 33686333》》new StringBuffer() 39710019》》new StringBuffer() 34394495》》new StringBuffer() 34933108》》new StringBuffer() 41481450》》new StringBuffer() 方式三 24382114》》delete(0, buffer.length()) 28005028》》delete(0, buffer.length()) 14795450》》delete(0, buffer.length()) 14201005》》delete(0, buffer.length()) 23715825》》delete(0, buffer.length()) 14937904》》delete(0, buffer.length()) 14788061》》delete(0, buffer.length()) 14045004》》delete(0, buffer.length()) 14287216》》delete(0, buffer.length()) 14813924》》delete(0, buffer.length()) 13874634》》delete(0, buffer.length()) 13862318》》delete(0, buffer.length()) 13894751》》delete(0, buffer.length()) 14214142》》delete(0, buffer.length()) 14727713》》delete(0, buffer.length()) 13853697》》delete(0, buffer.length()) 13840971》》delete(0, buffer.length()) 15115252》》delete(0, buffer.length()) 13505980》》delete(0, buffer.length()) 13438243》》delete(0, buffer.length())
时间: 2024-11-03 03:38:08