src.it.unimi.dsi.test.MutableStringLengthSpeedTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsiutils Show documentation
Show all versions of dsiutils Show documentation
The DSI utilities are a mishmash of classes accumulated during the last twenty years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.
package it.unimi.dsi.test;
import it.unimi.dsi.lang.MutableString;
public class MutableStringLengthSpeedTest {
private MutableStringLengthSpeedTest() {}
public static void main(final String[] arg) {
long i, n;
n = Long.parseLong(arg[0]);
MutableString s = new MutableString("foobar0");
MutableString t = new MutableString("foobar1");
String u = new String("foobar2");
StringBuffer v = new StringBuffer("foobar3");
StringBuilder w = new StringBuilder("foobar4");
int k = 10;
int x = 0;
while (k-- != 0) {
long start;
System.out.println();
start = -System.nanoTime();
i = n;
while (i-- != 0) x ^= u.length();
start += System.nanoTime();
System.out.println("Called length() " + n + " times on a string in " + start + " ns (" + start / (double)n + " ns/call)");
start = -System.nanoTime();
i = n;
while (i-- != 0) x ^= t.length();
start += System.nanoTime();
System.out.println("Called length() " + n + " times on a compact string in " + start + " ns (" + start / (double)n + " ns/call)");
start = -System.nanoTime();
i = n;
s.loose();
while (i-- != 0) x ^= s.length();
start += System.nanoTime();
System.out.println("Called length() " + n + " times on a loose string in " + start + " ns (" + start / (double)n + " ns/call)");
start = -System.nanoTime();
i = n;
while (i-- != 0) x ^= v.length();
start += System.nanoTime();
System.out.println("Called length() " + n + " times on a string buffer in " + start + " ns (" + start / (double)n + " ns/call)");
start = -System.nanoTime();
i = n;
while (i-- != 0) x ^= w.length();
start += System.nanoTime();
if (x == 0) System.out.println();
System.out.println("Called length() " + n + " times on a string builder in " + start + " ns (" + start / (double)n + " ns/call)");
}
}
}