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

src.it.unimi.dsi.test.MutableStringReplaceSpeedTest 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;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MutableStringReplaceSpeedTest {

	private MutableStringReplaceSpeedTest() {}

	public static void main(final String[] arg) throws IOException {

		String target = null;

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		MutableString ms = new MutableString();
		String line;
		while ((line = br.readLine()) != null) ms.append("\n").append(line);
		target = ms.toString();

		MutableString s;
		String st;
		final String searchString = arg[0];
		if (searchString.length() != 1) throw new IllegalArgumentException();
		final char searchChar = new MutableString(arg[0]).charAt(0);
		final String replaceString = arg[1];
		final MutableString replace = new MutableString(replaceString);
		final int n = Integer.parseInt(arg[2]);
		long start;

		for (int k = 10; k-- != 0;) {
			System.out.println();

			s = new MutableString(target).compact();
			start = -System.nanoTime();
			for(int i = n; i-- != 0;) s.replace(searchChar, replace);
			start += System.nanoTime();
			System.out.println("Called replace() " + n + " times on a compact string in " + start + " ns (" + start / (double)n + " ns/call)");

			s = new MutableString(target).loose();
			start = -System.nanoTime();
			for(int i = n; i-- != 0;) s.replace(searchChar, replace);
			start += System.nanoTime();
			System.out.println("Called replace() " + n + " times on a loose string in " + start + " ns (" + start / (double)n + " ns/call)");

			final StringBuilder sb = new StringBuilder(target);
			start = -System.nanoTime();

			for(int i = n; i-- != 0;) {
				int j = sb.length();
				for (;;) {
					j = sb.lastIndexOf(searchString, j);
					if (j == -1) break;
					sb.replace(j, j + 1, replaceString);
					j--;
				}
			}

			start += System.nanoTime();
			System.out.println("Called replace() " + n + " times on a string builder in " + start + " ns (" + start / (double)n + " ns/call)");
			assert sb.length() == s.length();
			assert s.toString().equals(sb.toString());

			st = new String(target);
			start = -System.nanoTime();
			for(int i = n; i-- != 0;) st = st.replaceAll(searchString, replaceString);
			start += System.nanoTime();
			System.out.println("Called replaceAll() " + n + " times on a string in " + start + " ns (" + start / (double)n + " ns/call)");
			assert sb.length() == st.length();
			assert st.equals(sb.toString());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy