All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.it.unimi.dsi.test.MutableStringLengthSpeedTest Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.7.3
Show newest version
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)");
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy