Advertisement
Guest User

CPerkins

a guest
Jun 5th, 2010
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.98 KB | None | 0 0
  1. package com.pragmaticsoftwaredevelopment.timingstringassembly;
  2.  
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.io.StringWriter;
  7. import java.util.Random;
  8.  
  9.  
  10. public class TimingStringAssembly {
  11.    String bigWodgeOfText;
  12.    {
  13.       Random rand = new Random();
  14.       String characters=new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+-=[]{};:',.<>/?| abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  15.       char[] buff = new char[100000];
  16.       for (int ii=0; ii < buff.length; ii++)
  17.          buff[ii] = characters.charAt(rand.nextInt(characters.length() -1));
  18.       bigWodgeOfText = new String(buff);
  19.    }
  20.  
  21.    public String randomLine (Random rand, int lineWidth) {
  22.       int start = rand.nextInt(bigWodgeOfText.length()-lineWidth-1);
  23.       String line = bigWodgeOfText.substring(start, start+lineWidth);
  24.       return line;
  25.    }
  26.  
  27.    public static String newline = System.getProperty("line.separator");
  28.    public void justMakeTheRandomLines (int seed, int maxLines, int maxLineWidth) {
  29.       Random rand = new Random(seed);
  30.       int numLines = rand.nextInt(maxLines);
  31.       int lineWidth = rand.nextInt(maxLineWidth);
  32.       for (int ii=0; ii < numLines; ii++) {
  33.          String randLine = randomLine (rand, lineWidth); // but will this be optimized away?
  34.       }
  35.    }
  36.    public String viaStringBuilder (int seed, int maxLines, int maxLineWidth) {
  37.       Random rand = new Random(seed);
  38.       StringBuilder builder = new StringBuilder();
  39.       int numLines = rand.nextInt(maxLines);
  40.       int lineWidth = rand.nextInt(maxLineWidth);
  41.       for (int ii=0; ii < numLines; ii++) {
  42.          builder.append(randomLine (rand, lineWidth) + newline);
  43.       }
  44.       return builder.toString();
  45.    }
  46.  
  47.    public String viaWriters(int seed, int maxLines, int maxLineWidth) {
  48.       Random rand = new Random(seed);
  49.       StringWriter stringWriter = new StringWriter();
  50.       PrintWriter printWriter = new PrintWriter(stringWriter);
  51.       int numLines = rand.nextInt(maxLines);
  52.       int lineWidth = rand.nextInt(maxLineWidth);
  53.       for (int ii=0; ii < numLines; ii++) {
  54.          printWriter.println(randomLine (rand, lineWidth));
  55.       }
  56.       printWriter.flush();
  57.       printWriter.close();
  58.       return stringWriter.toString();
  59.    }
  60.  
  61.  
  62.    public void doit()
  63.    {
  64.       // WARMUP: call each method several times to make sure any optimizations have a chance to kick in:
  65.       int warmupTimes = 1000;
  66.       int seed=2317;
  67.       int maxLines=100;
  68.       int maxLineWidth=200;
  69.       try {
  70.          for (int jj=0; jj < warmupTimes; jj++) {
  71.             FileWriter writer1 = new FileWriter ("C:\\temp\\junk_byBuilder");
  72.             FileWriter writer2 = new FileWriter ("C:\\temp\\junk_byWriter");
  73.             justMakeTheRandomLines (seed, maxLines, maxLineWidth);  // I do suspect this is optimized away, but it's here for comparison anyway
  74.             writer1.append(viaStringBuilder (seed, maxLines, maxLineWidth));
  75.             writer2.append (viaWriters (seed, maxLines, maxLineWidth));
  76.             seed *= 2317;
  77.             writer1.flush(); writer1.close();
  78.             writer2.flush(); writer2.close();
  79.          }
  80.       } catch (IOException e) {
  81.          String msg = "IOException during warmup: [" + e.getLocalizedMessage() + "]";
  82.          System.out.println (msg);
  83.          e.printStackTrace();
  84.          System.out.flush();
  85.          System.exit(0);
  86.  
  87.       }
  88.  
  89.       // TESTING: Now, for the money
  90.       int numberOfLoops=1000;
  91.       int numberOfTestsPerLoop=100;
  92.       int totalTests = numberOfLoops * numberOfTestsPerLoop;
  93.  
  94.       long totalMsJustCreate=0;
  95.       long totalMsByStringBuilder=0;
  96.       long totalMsByWriters=0;
  97.       try {
  98.          for (int jj=0; jj < numberOfLoops; jj++) {
  99.             System.out.println ("looptop " + jj);
  100.             FileWriter writer1 = new FileWriter ("C:\\temp\\junk_byBuilder");
  101.             FileWriter writer2 = new FileWriter ("C:\\temp\\junk_byWriter");
  102.  
  103.             for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
  104.                long start = System.currentTimeMillis();
  105.                justMakeTheRandomLines (seed, maxLines, maxLineWidth);  // I do suspect this is optimized away, but it's here for comparison anyway
  106.                totalMsJustCreate += (System.currentTimeMillis() - start);
  107.             }
  108.             for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
  109.                long start = System.currentTimeMillis();
  110.                writer1.append(viaStringBuilder (seed, maxLines, maxLineWidth));
  111.                totalMsByStringBuilder += (System.currentTimeMillis() - start);
  112.             }
  113.             for (int ii=0; ii < numberOfTestsPerLoop; ii++) {
  114.                long start = System.currentTimeMillis();
  115.                writer2.append (viaWriters (seed, maxLines, maxLineWidth));
  116.                totalMsByWriters += (System.currentTimeMillis() - start);
  117.             }
  118.             seed *= 2317;
  119.             writer1.flush(); writer1.close();
  120.             writer2.flush(); writer2.close();
  121.          }
  122.          System.out.println ("After the testing (" + totalTests + " of each method), we see that it took:");
  123.          System.out.println ("    " + totalMsJustCreate + " ms to simply create the random strings (probably optimized away)");
  124.          System.out.println ("    " + totalMsByStringBuilder + " ms to assemble the random strings into one using StringBuilder");
  125.          System.out.println ("    " + totalMsByWriters + " ms to assemble the random strings into one using PrintWriter(StringWriter)");
  126.       } catch (IOException e) {
  127.          String msg = "IOException: [" + e.getLocalizedMessage() + "]";
  128.          System.out.println (msg);
  129.          e.printStackTrace();
  130.          System.out.flush();
  131.  
  132.       }
  133.  
  134.  
  135.    }
  136.  
  137.    public static void main(String[] args)
  138.    {
  139.       TimingStringAssembly timer = new TimingStringAssembly();
  140.       timer.doit();
  141.    }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement