Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pragmaticsoftwaredevelopment.timingstringassembly;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.util.Random;
- public class TimingStringAssembly {
- String bigWodgeOfText;
- {
- Random rand = new Random();
- String characters=new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+-=[]{};:',.<>/?| abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
- char[] buff = new char[100000];
- for (int ii=0; ii < buff.length; ii++)
- buff[ii] = characters.charAt(rand.nextInt(characters.length() -1));
- bigWodgeOfText = new String(buff);
- }
- public String randomLine (Random rand, int lineWidth) {
- int start = rand.nextInt(bigWodgeOfText.length()-lineWidth-1);
- String line = bigWodgeOfText.substring(start, start+lineWidth);
- return line;
- }
- public static String newline = System.getProperty("line.separator");
- public void justMakeTheRandomLines (int seed, int maxLines, int maxLineWidth) {
- Random rand = new Random(seed);
- int numLines = rand.nextInt(maxLines);
- int lineWidth = rand.nextInt(maxLineWidth);
- for (int ii=0; ii < numLines; ii++) {
- String randLine = randomLine (rand, lineWidth); // but will this be optimized away?
- }
- }
- public String viaStringBuilder (int seed, int maxLines, int maxLineWidth) {
- Random rand = new Random(seed);
- StringBuilder builder = new StringBuilder();
- int numLines = rand.nextInt(maxLines);
- int lineWidth = rand.nextInt(maxLineWidth);
- for (int ii=0; ii < numLines; ii++) {
- builder.append(randomLine (rand, lineWidth) + newline);
- }
- return builder.toString();
- }
- public String viaWriters(int seed, int maxLines, int maxLineWidth) {
- Random rand = new Random(seed);
- StringWriter stringWriter = new StringWriter();
- PrintWriter printWriter = new PrintWriter(stringWriter);
- int numLines = rand.nextInt(maxLines);
- int lineWidth = rand.nextInt(maxLineWidth);
- for (int ii=0; ii < numLines; ii++) {
- printWriter.println(randomLine (rand, lineWidth));
- }
- printWriter.flush();
- printWriter.close();
- return stringWriter.toString();
- }
- public void doit()
- {
- // WARMUP: call each method several times to make sure any optimizations have a chance to kick in:
- int warmupTimes = 1000;
- int seed=2317;
- int maxLines=100;
- int maxLineWidth=200;
- try {
- for (int jj=0; jj < warmupTimes; jj++) {
- FileWriter writer1 = new FileWriter ("C:\\temp\\junk_byBuilder");
- FileWriter writer2 = new FileWriter ("C:\\temp\\junk_byWriter");
- justMakeTheRandomLines (seed, maxLines, maxLineWidth); // I do suspect this is optimized away, but it's here for comparison anyway
- writer1.append(viaStringBuilder (seed, maxLines, maxLineWidth));
- writer2.append (viaWriters (seed, maxLines, maxLineWidth));
- seed *= 2317;
- writer1.flush(); writer1.close();
- writer2.flush(); writer2.close();
- }
- } catch (IOException e) {
- String msg = "IOException during warmup: [" + e.getLocalizedMessage() + "]";
- System.out.println (msg);
- e.printStackTrace();
- System.out.flush();
- System.exit(0);
- }
- // TESTING: Now, for the money
- int numberOfLoops=1000;
- int numberOfTestsPerLoop=100;
- int totalTests = numberOfLoops * numberOfTestsPerLoop;
- long totalMsJustCreate=0;
- long totalMsByStringBuilder=0;
- long totalMsByWriters=0;
- try {
- for (int jj=0; jj < numberOfLoops; jj++) {
- System.out.println ("looptop " + jj);
- FileWriter writer1 = new FileWriter ("C:\\temp\\junk_byBuilder");
- FileWriter writer2 = new FileWriter ("C:\\temp\\junk_byWriter");
- for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
- long start = System.currentTimeMillis();
- justMakeTheRandomLines (seed, maxLines, maxLineWidth); // I do suspect this is optimized away, but it's here for comparison anyway
- totalMsJustCreate += (System.currentTimeMillis() - start);
- }
- for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
- long start = System.currentTimeMillis();
- writer1.append(viaStringBuilder (seed, maxLines, maxLineWidth));
- totalMsByStringBuilder += (System.currentTimeMillis() - start);
- }
- for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
- long start = System.currentTimeMillis();
- writer2.append (viaWriters (seed, maxLines, maxLineWidth));
- totalMsByWriters += (System.currentTimeMillis() - start);
- }
- seed *= 2317;
- writer1.flush(); writer1.close();
- writer2.flush(); writer2.close();
- }
- System.out.println ("After the testing (" + totalTests + " of each method), we see that it took:");
- System.out.println (" " + totalMsJustCreate + " ms to simply create the random strings (probably optimized away)");
- System.out.println (" " + totalMsByStringBuilder + " ms to assemble the random strings into one using StringBuilder");
- System.out.println (" " + totalMsByWriters + " ms to assemble the random strings into one using PrintWriter(StringWriter)");
- } catch (IOException e) {
- String msg = "IOException: [" + e.getLocalizedMessage() + "]";
- System.out.println (msg);
- e.printStackTrace();
- System.out.flush();
- }
- }
- public static void main(String[] args)
- {
- TimingStringAssembly timer = new TimingStringAssembly();
- timer.doit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement