Skilled Illusionist
- Joined
- Apr 21, 2012
- Messages
- 337
- Reaction score
- 144
Test 1: Constant Sample Set
Results:
N.b. nanosecond times are simply estimates, they are not necessarily precise.
How was this conducted?
Code:
Test 2: Constant Sample Set Improved
Results:
N.b. nanosecond times are simply estimates, they are not necessarily precise.
How was this conducted?
Code:
I may add more tests in the future, as this one only deals with constant sets of data, of constant size.
N.b. these tests are implementation-dependent, and are really just suggestions, more than they are statements of fact.
Results:
Code:
string: strswitch bench: 1028 ms 1027517000 ns
string: enumswitch bench: 902 ms 901389000 ns
string: strequals bench: 935 ms 935431000 ns
string: strequalsic bench: 1116 ms 1115405000 ns
How was this conducted?
A set of 16 strings, all with a length of 6, is prepared. Each is added into a collection of strings, and the collection then shuffles itself randomly. The first string after shuffling it is then compared to using the desired means. The collection is then reshuffled, and the process begins again. This process is iterated over 1,000,000 times.
Code:
Code:
package benchmarking;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* A tool for benchmarking different ways of comparing strings.
* @author Aaron Weiss
* @version 1.0
*/
public class StringBenchmark {
private static final int ITERATIONS = 1000000;
private static Scanner in;
/**
* @param args None
*/
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
for (Strings s : Strings.values()) {
strings.add(s.name());
}
Collections.shuffle(strings);
in = new Scanner(System.in);
System.out.print("Press return to begin.");
in.nextLine();
in.close();
{
// System.out.println("string: strswitch bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Collections.shuffle(strings);
switch (strings.get(0)) {
case "FLIGHT":
case "TREATS":
case "TEASES":
case "BASTES":
case "TASTES":
case "WASTES":
case "TATERS":
case "HATERS":
case "HEATER":
case "READER":
case "FASTER":
case "HASTES":
case "FASTED":
case "PASTED":
case "PASTER":
case "BASTED":
break;
}
Collections.shuffle(strings);
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strswitch bench: end");
System.out.println("string: strswitch bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: enumswitch bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Collections.shuffle(strings);
Strings s = Strings.valueOf(strings.get(0));
switch (s) {
case FLIGHT:
case TREATS:
case TEASES:
case BASTES:
case TASTES:
case WASTES:
case TATERS:
case HATERS:
case HEATER:
case READER:
case FASTER:
case HASTES:
case FASTED:
case PASTED:
case PASTER:
case BASTED:
break;
}
Collections.shuffle(strings);
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: enumswitch bench: end");
System.out.println("string: enumswitch bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: strequals bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Collections.shuffle(strings);
String compare = strings.get(0);
if (compare.equals("FLIGHT")) {
// pass
} else if (compare.equals("TREATS")) {
// pass
} else if (compare.equals("TEASES")) {
// pass
} else if (compare.equals("BASTES")) {
// pass
} else if (compare.equals("TASTES")) {
// pass
} else if (compare.equals("WASTES")) {
// pass
} else if (compare.equals("TATERS")) {
// pass
} else if (compare.equals("HATERS")) {
// pass
} else if (compare.equals("HEATER")) {
// pass
} else if (compare.equals("READER")) {
// pass
} else if (compare.equals("FASTER")) {
// pass
} else if (compare.equals("HASTES")) {
// pass
} else if (compare.equals("FASTED")) {
// pass
} else if (compare.equals("PASTED")) {
// pass
} else if (compare.equals("PASTER")) {
// pass
} else if (compare.equals("BASTED")) {
// pass
}
Collections.shuffle(strings);
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strequals bench: end");
System.out.println("string: strequals bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: strequalsic bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Collections.shuffle(strings);
String compare = strings.get(0);
if (compare.equals("FLIGHT")) {
// pass
} else if (compare.equalsIgnoreCase("TREATS")) {
// pass
} else if (compare.equalsIgnoreCase("TEASES")) {
// pass
} else if (compare.equalsIgnoreCase("BASTES")) {
// pass
} else if (compare.equalsIgnoreCase("TASTES")) {
// pass
} else if (compare.equalsIgnoreCase("WASTES")) {
// pass
} else if (compare.equalsIgnoreCase("TATERS")) {
// pass
} else if (compare.equalsIgnoreCase("HATERS")) {
// pass
} else if (compare.equalsIgnoreCase("HEATER")) {
// pass
} else if (compare.equalsIgnoreCase("READER")) {
// pass
} else if (compare.equalsIgnoreCase("FASTER")) {
// pass
} else if (compare.equalsIgnoreCase("HASTES")) {
// pass
} else if (compare.equalsIgnoreCase("FASTED")) {
// pass
} else if (compare.equalsIgnoreCase("PASTED")) {
// pass
} else if (compare.equalsIgnoreCase("PASTER")) {
// pass
} else if (compare.equalsIgnoreCase("BASTED")) {
// pass
}
Collections.shuffle(strings);
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strequalsic bench: end");
System.out.println("string: strequalsic bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
}
private enum Strings {
FLIGHT,
TREATS,
TEASES,
BASTES,
TASTES,
WASTES,
TATERS,
HATERS,
HEATER,
READER,
FASTER,
HASTES,
FASTED,
PASTED,
PASTER,
BASTED,
};
}
Test 2: Constant Sample Set Improved
Results:
Code:
string: strswitch bench: 163 ms 163141000 ns
string: enumswitch bench: 134 ms 134133000 ns
string: strequals bench: 118 ms 118052000 ns
string: strequalsic bench: 259 ms 259730000 ns
How was this conducted?
A set of 16 strings, all with a length of 6, is prepared. Each is added into a collection of strings. A random string from the list is then compared to using the desired means. Afterward, the process begins again. This process is iterated over 1,000,000 times.
Code:
Code:
package benchmarking;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
* A tool for benchmarking different ways of comparing strings.
* @author Aaron Weiss
* @version 2.0
*/
public class StringBenchmark {
private static final int ITERATIONS = 1000000;
private static Random randomizer;
private static Scanner in;
/**
* @param args None
*/
public static void main(String[] args) {
randomizer = new Random();
ArrayList<String> strings = new ArrayList<String>();
for (Strings s : Strings.values()) {
strings.add(s.name());
}
in = new Scanner(System.in);
System.out.print("Press return to begin.");
in.nextLine();
in.close();
{
// System.out.println("string: strswitch bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
switch (strings.get(randomizer.nextInt(Integer.MAX_VALUE) % strings.size())) {
case "FLIGHT":
case "TREATS":
case "TEASES":
case "BASTES":
case "TASTES":
case "WASTES":
case "TATERS":
case "HATERS":
case "HEATER":
case "READER":
case "FASTER":
case "HASTES":
case "FASTED":
case "PASTED":
case "PASTER":
case "BASTED":
break;
}
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strswitch bench: end");
System.out.println("string: strswitch bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: enumswitch bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Strings s = Strings.valueOf(strings.get(randomizer.nextInt(Integer.MAX_VALUE) % strings.size()));
switch (s) {
case FLIGHT:
case TREATS:
case TEASES:
case BASTES:
case TASTES:
case WASTES:
case TATERS:
case HATERS:
case HEATER:
case READER:
case FASTER:
case HASTES:
case FASTED:
case PASTED:
case PASTER:
case BASTED:
break;
}
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: enumswitch bench: end");
System.out.println("string: enumswitch bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: strequals bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
String compare = strings.get(randomizer.nextInt(Integer.MAX_VALUE) % strings.size());
if (compare.equals("FLIGHT")) {
// pass
} else if (compare.equals("TREATS")) {
// pass
} else if (compare.equals("TEASES")) {
// pass
} else if (compare.equals("BASTES")) {
// pass
} else if (compare.equals("TASTES")) {
// pass
} else if (compare.equals("WASTES")) {
// pass
} else if (compare.equals("TATERS")) {
// pass
} else if (compare.equals("HATERS")) {
// pass
} else if (compare.equals("HEATER")) {
// pass
} else if (compare.equals("READER")) {
// pass
} else if (compare.equals("FASTER")) {
// pass
} else if (compare.equals("HASTES")) {
// pass
} else if (compare.equals("FASTED")) {
// pass
} else if (compare.equals("PASTED")) {
// pass
} else if (compare.equals("PASTER")) {
// pass
} else if (compare.equals("BASTED")) {
// pass
}
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strequals bench: end");
System.out.println("string: strequals bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
{
// System.out.println("string: strequalsic bench: start");
long startNano = System.nanoTime();
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
String compare = strings.get(randomizer.nextInt(Integer.MAX_VALUE) % strings.size());
if (compare.equals("FLIGHT")) {
// pass
} else if (compare.equalsIgnoreCase("TREATS")) {
// pass
} else if (compare.equalsIgnoreCase("TEASES")) {
// pass
} else if (compare.equalsIgnoreCase("BASTES")) {
// pass
} else if (compare.equalsIgnoreCase("TASTES")) {
// pass
} else if (compare.equalsIgnoreCase("WASTES")) {
// pass
} else if (compare.equalsIgnoreCase("TATERS")) {
// pass
} else if (compare.equalsIgnoreCase("HATERS")) {
// pass
} else if (compare.equalsIgnoreCase("HEATER")) {
// pass
} else if (compare.equalsIgnoreCase("READER")) {
// pass
} else if (compare.equalsIgnoreCase("FASTER")) {
// pass
} else if (compare.equalsIgnoreCase("HASTES")) {
// pass
} else if (compare.equalsIgnoreCase("FASTED")) {
// pass
} else if (compare.equalsIgnoreCase("PASTED")) {
// pass
} else if (compare.equalsIgnoreCase("PASTER")) {
// pass
} else if (compare.equalsIgnoreCase("BASTED")) {
// pass
}
}
long end = System.currentTimeMillis();
long endNano = System.nanoTime();
// System.out.println("string: strequalsic bench: end");
System.out.println("string: strequalsic bench: " + (end - start) + " ms " + (endNano - startNano) + " ns");
}
}
private enum Strings {
FLIGHT,
TREATS,
TEASES,
BASTES,
TASTES,
WASTES,
TATERS,
HATERS,
HEATER,
READER,
FASTER,
HASTES,
FASTED,
PASTED,
PASTER,
BASTED,
};
}
I may add more tests in the future, as this one only deals with constant sets of data, of constant size.
N.b. these tests are implementation-dependent, and are really just suggestions, more than they are statements of fact.
Ignorant? Going to comment something stupid? Have some bunnies instead.
Last edited: