Thursday 17 November 2011

Improve performance by switching from StringBuffer to StringBuilder

If you have a lot of old code and you are pritty sure that you do not use StringBuffer objects shared with different threads, you can upgrade all your project's code, changing StringBuffer with StringBuilder. The advantages could be like 35% of effeciency increment based on this article.

Here you are a couple of Unix scripts for this.

The first script just produces the list of files to be modified.

grep -l -R 'StringBuffer' ./project/src/* > files-to-change.txt

The second script replaces all StringBuffer with StringBuilder, creating .bak file for every modified file.

while read CUR_FILE
do
    echo "$CUR_FILE"
    mv $CUR_FILE $CUR_FILE.bak
    sed 's/StringBuffer/StringBuilder/g' $CUR_FILE.bak > $CUR_FILE
done < files-to-change.txt

No comments: