Last active
October 3, 2017 02:34
-
-
Save Daomephsta/d7c0da865dbbbf11615a2f2357d49d3f to your computer and use it in GitHub Desktop.
String Hashcode Performance
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
| package leviathan143.stringhashtest; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Random; | |
| public class Main | |
| { | |
| private static final char[] ALPHABET = "abcdefghijklmnopqrstuvwxyz".toCharArray(); | |
| private static final Random rand = new Random(); | |
| private static int minLen = 1; | |
| private static int maxLen = 3; | |
| private static int iterations = 10000; | |
| public static void main(String[] args) | |
| { | |
| parseArgs(args); | |
| for(int l = minLen; l <= maxLen; l++) | |
| { | |
| List<String> generatedStrings = new ArrayList<String>(iterations); | |
| for(int i = 0; i < iterations; i++) | |
| { | |
| generatedStrings.add(generateString(l)); | |
| } | |
| List<Long> stringHashTimes = new ArrayList<Long>(iterations); | |
| for(String string : generatedStrings) | |
| { | |
| long startTime = System.nanoTime(); | |
| string.hashCode(); | |
| long finishTime = System.nanoTime() - startTime; | |
| stringHashTimes.add(finishTime); | |
| } | |
| System.out.println("Avg. hash time for " + iterations + " randomly generated strings of length " + l + ": " + computeAvgTime(stringHashTimes) + " ns"); | |
| } | |
| } | |
| private static void parseArgs(String[] args) | |
| { | |
| String nextArg = ""; | |
| for(int a = 0; a < args.length; a++) | |
| { | |
| if(args[a].startsWith("-")) | |
| { | |
| if((a + 1) < args.length) nextArg = args[a + 1]; | |
| else System.out.println("Missing parameter for argument " + args[a]); | |
| switch (args[a]) | |
| { | |
| case "-min": | |
| minLen = Integer.parseInt(nextArg); | |
| break; | |
| case "-max": | |
| maxLen = Integer.parseInt(nextArg); | |
| break; | |
| case "--I": | |
| case "-iterations": | |
| iterations = Integer.parseInt(nextArg); | |
| break; | |
| default: | |
| System.out.println("Unknown argument: " + args[a]); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| private static String generateString(int length) | |
| { | |
| char[] stringChars = new char[length]; | |
| for(int c = 0; c < length; c++) | |
| { | |
| stringChars[c] = ALPHABET[rand.nextInt(ALPHABET.length)]; | |
| } | |
| return String.valueOf(stringChars); | |
| } | |
| private static long computeAvgTime(List<Long> times) | |
| { | |
| long sum = 0; | |
| for(long time : times) | |
| { | |
| sum += time; | |
| } | |
| return sum / iterations; | |
| } | |
| } |
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
| A length of 16 is the best case scenario(1 letter modid + 3 3 letter hashes and "horse/") | |
| A length of 79 is the worst case scenario(64 letter modid + 3 3 letter hashes and "horse/") | |
| Generated with args: -min 16 -max 79 --I 1000000 | |
| Avg. hash time for 1000000 randomly generated strings of length 16: 19 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 17: 31 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 18: 29 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 19: 19 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 20: 24 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 21: 35 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 22: 27 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 23: 25 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 24: 20 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 25: 32 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 26: 25 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 27: 39 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 28: 35 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 29: 37 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 30: 36 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 31: 30 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 32: 24 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 33: 32 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 34: 30 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 35: 37 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 36: 43 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 37: 49 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 38: 39 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 39: 39 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 40: 32 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 41: 31 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 42: 47 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 43: 47 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 44: 46 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 45: 46 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 46: 46 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 47: 55 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 48: 61 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 49: 57 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 50: 56 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 51: 45 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 52: 36 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 53: 47 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 54: 55 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 55: 52 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 56: 61 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 57: 65 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 58: 58 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 59: 57 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 60: 59 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 61: 62 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 62: 62 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 63: 65 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 64: 66 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 65: 69 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 66: 70 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 67: 71 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 68: 74 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 69: 92 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 70: 78 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 71: 56 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 72: 66 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 73: 74 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 74: 77 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 75: 74 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 76: 77 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 77: 82 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 78: 82 ns | |
| Avg. hash time for 1000000 randomly generated strings of length 79: 80 ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment