Created
December 4, 2015 19:07
-
-
Save jalex19100/c1e59b1f130b81b1119f to your computer and use it in GitHub Desktop.
Really old class to test String performance in Java. Wasn't that helpful.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.StringTokenizer; | |
| import java.util.regex.Pattern; | |
| /** | |
| * Class: StringPerformance | |
| */ | |
| public class StringPerformance { | |
| public static void main(String args[]) { | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = 100000; i < 100000 + 60; i++) { | |
| sb.append(i). | |
| append(' '); | |
| } | |
| String sample = sb.toString(); | |
| int runs = 100000; | |
| for (int i = 0; i < 5; i++) | |
| { | |
| { | |
| long start = System.nanoTime(); | |
| for (int r = 0; r < runs; r++) { | |
| StringTokenizer st = new StringTokenizer(sample); | |
| List<String> list = new ArrayList<String>(); | |
| while (st.hasMoreTokens()) { | |
| list.add(st.nextToken()); | |
| } | |
| } | |
| long time = System.nanoTime() - start; | |
| System.out.printf("StringTokenizer took an average of %.1f us%n", time / runs / 1000.0); | |
| } | |
| { | |
| long start = System.nanoTime(); | |
| Pattern spacePattern = Pattern.compile(" "); | |
| for (int r = 0; r < runs; r++) { | |
| List<String> list = Arrays.asList(spacePattern.split(sample, 0)); | |
| } | |
| long time = System.nanoTime() - start; | |
| System.out.printf("Pattern.split took an average of %.1f us%n", time / runs / 1000.0); | |
| } | |
| { | |
| long start = System.nanoTime(); | |
| for (int r = 0; r < runs; r++) { | |
| List<String> list = new ArrayList<String>(); | |
| int pos = 0, end; | |
| while ((end = sample.indexOf(' ', pos)) >= 0) { | |
| list.add(sample.substring(pos, end)); | |
| pos = end + 1; | |
| } | |
| } | |
| long time = System.nanoTime() - start; | |
| System.out.printf("indexOf loop took an average of %.1f us%n", time / runs / 1000.0); | |
| } | |
| } | |
| runs = 10000000; | |
| long totalTime = 0; | |
| for (int i = 0; i < runs; i++) { | |
| String val = "" + Math.random(); | |
| long start = System.nanoTime(); | |
| float f = Float.parseFloat(val); | |
| long end = System.nanoTime(); | |
| //System.err.println(f); | |
| totalTime += (end - start); | |
| } | |
| long time = totalTime / runs; | |
| totalTime = 0; | |
| for (int i = 0; i < runs; i++) { | |
| String val = "" + Math.random(); | |
| long start = System.nanoTime(); | |
| double d = Double.parseDouble(val); | |
| long end = System.nanoTime(); | |
| //System.err.println(d); | |
| totalTime += (end - start); | |
| } | |
| long time2 = totalTime / runs; | |
| System.out.println("Average Float.parseFloat() time was " + time + " ns."); | |
| System.out.println("Average Double.parseDouble() time was " + time2 + " ns."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment