欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

StringBuffer中delete与setLength清空字符串效率比较

程序员文章站 2024-01-26 15:25:58
问题: StringBuffer中有delete、setLength两个方法可以快速清空字符数组。哪个效率高呢? 结论:从清空字符串角度看,两者效率都很高,比较来看,setLength效率更高。 分析如下: 这两个函数都是继承自AbstractStringBuilder类。函数原型如下: delet ......

问题:

        stringbuffer中有delete、setlength两个方法可以快速清空字符数组。哪个效率高呢?

结论:从清空字符串角度看,两者效率都很高,比较来看,setlength效率更高

分析如下:

        这两个函数都是继承自abstractstringbuilder类。函数原型如下:  

       public abstractstringbuilder delete(int start, int end) ;
       public void setlength(int newlength) ;

delete(int start, int end)删除了start、end之间的字符,并返回新的字符串。

setlength(int newlength)重新设置了字符长度,如果newlength比原长度大,则新增的空间赋值为‘\0’。

两者用途不同,却都可以用于清空字符串。delete(0,abstractstringbuilder.length) 与setlength(0)可以达到相同的效果。

比较其源代码之后,发现setlength(0)在清空代码时只是对长度做了 一次赋值,setlength除了对长度赋值外,还做了一次代码copy操作。多执行的代码如下:

                 system.arraycopy(value, start+len, value, start, count-end);

因为count = end;所以这个copy应该也不会执行。但是delete函数多做了一些判断,效率上回比setlength低一点。不过delete多出的条件判断对性能的影响是微乎其微的,通过代码测试没有明显的差异,两者在清空字符串时效率都非常高

另外,这个两个函数虽然可以将字符串清空,但并没有将资源回收,也就是说并没有达到回收资源的效果,因为abstractstringbuilder 的字符数组仍在内存中,只不过我们人为将数组的有效长度置位了0,数组所占的资源并没有及时释放,只能等java的垃圾回收进行释放。

jdk1.8源代码如下(jdk1.6与之类似,以上结论不变)

public abstractstringbuilder delete(int start, int end) {

        if (start < 0)
            throw new stringindexoutofboundsexception(start);

        if (end > count)
            end = count;

        if (start > end)
            throw new stringindexoutofboundsexception();

        int len = end - start;
        if (len > 0) {
            system.arraycopy(value, start+len, value, start, count-end);
            count -= len;
        }
        return this;

    }   

    public void setlength(int newlength) {

        if (newlength < 0)
            throw new stringindexoutofboundsexception(newlength);
ensurecapacityinternal(newlength); if (count < newlength) { arrays.fill(value, count, newlength, '\0'); } count = newlength; }
    

 测试代码:

package mystring;
public class testmain {

    public static void main(string[] args) {
        teststringbufferclear();
    }

    private static void teststringbufferclear() {
        stringbuffer sbf = new  stringbuffer("wwwwww");
        stringbuffer sbi = new  stringbuffer("wwwwww");
        int count = 1000000;
        long start ;
        long end;

        stringbuffer sbftest = new stringbuffer(); 
        for(int i = 0; i < 1000; i++)
        {
                sbftest.append("1234567890");
        }
        string str = sbftest.tostring();
             
        start = system.currenttimemillis();        
        for (int i = 0; i < count; i++) {
            sbi.append(str);
            sbi.setlength(0);

        }
        end = system.currenttimemillis();
        system.out.println("stringbuffer--setlength:" + (end - start));              

start = system.currenttimemillis(); for (int i = 0; i < count; i++) { sbf.append(str); sbf.delete(0, sbf.length()); } end = system.currenttimemillis(); system.out.println("stringbuffer--delete:" + (end - start));
start = system.currenttimemillis(); for (int i = 0; i < count; i++) { sbf.append(str); sbf = new stringbuffer("123431431"); } end = system.currenttimemillis(); system.out.println("stringbuffer--new stringbuffer:" + (end - start)); } }

测试结果:

stringbuffer--setlength:674
stringbuffer--delete:689
stringbuffer--new stringbuffer:4108