alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

//      Copyright 1996-1999, International Business Machines 
//      Corporation. All Rights Reserved.

package org.apache.log4j.performance;

import java.util.Date;

/**
   Measure difference in performance of string concatenation versus
   creating an anonymous string array.

   

You should be able to see that anonymous string array construction is significatnly faster than string concatenation. The difference increases proportionally with the length of the strings to be concatanated. @author Ceki Gülcü */ public class ConcatVsArray { static void Usage() { System.err.println("Usage: java org.apache.log4j.performance.ConcatVsArray " + "string1 string2 runLength\n" + " where runLength is an integer."); System.exit(1); } public static void main(String args[]) { if(args.length != 3) { Usage(); } String s1 = args[0]; String s2 = args[1]; int runLength = 0; try { runLength = Integer.parseInt(args[2]); } catch(java.lang.NumberFormatException e) { System.err.println(e); Usage(); } double micros; String[] sa; long before = new Date().getTime(); for(int i = 0; i < runLength; i++) { sa = new String[]{s1, s2}; } micros = (new Date().getTime() - before)*1000.0/runLength; System.out.println("The anonymous array loop took around " + micros + " microseconds."); String s; before = new Date().getTime(); for(int i = 0; i < runLength; i++) { s = s1 + s2; } micros = (new Date().getTime() - before)*1000.0/runLength; System.out.println("The append loop took around " + micros + " microseconds."); } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.