Created
September 25, 2012 15:47
-
-
Save mattnworb/3782694 to your computer and use it in GitHub Desktop.
Benchmark for http://stackoverflow.com/questions/12585288/a-fast-way-to-find-unique-values-in-the-list/12585320#12585464
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.Collection; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Set; | |
import java.util.concurrent.TimeUnit; | |
public class Benchmark { | |
public static void main(String[] args) { | |
int rounds = 10000; | |
int size = 5000; | |
List<KVPair> testData = generateRandom(size); | |
System.out.println("Starting benchmark of u1"); | |
long start = System.nanoTime(); | |
for (int i = 0; i < rounds; i++) { | |
Set<String> s = u1(testData); | |
} | |
long ellapsed = System.nanoTime() - start; | |
long ms = TimeUnit.MILLISECONDS.convert(ellapsed, TimeUnit.NANOSECONDS); | |
System.out.printf("%d iterations took %s ms\n", rounds, ms); | |
System.out.printf("Mean iteration time %.3f ms\n", (1.0 * ms / rounds)); | |
System.out.println("\nStarting benchmark of u2"); | |
start = System.nanoTime(); | |
for (int i = 0; i < rounds; i++) { | |
Collection<String> s = u2(testData); | |
} | |
ellapsed = System.nanoTime() - start; | |
ms = TimeUnit.MILLISECONDS.convert(ellapsed, TimeUnit.NANOSECONDS); | |
System.out.printf("%d iterations took %s ms\n", rounds, ms); | |
System.out.printf("Mean iteration time %.3f ms\n", (1.0 * ms / rounds)); | |
System.out.println("\nStarting benchmark of u3"); | |
start = System.nanoTime(); | |
for (int i = 0; i < rounds; i++) { | |
Collection<String> s = u3(testData); | |
} | |
ellapsed = System.nanoTime() - start; | |
ms = TimeUnit.MILLISECONDS.convert(ellapsed, TimeUnit.NANOSECONDS); | |
System.out.printf("%d iterations took %s ms\n", rounds, ms); | |
System.out.printf("Mean iteration time %.3f ms\n", (1.0 * ms / rounds)); | |
System.out.println("Done"); | |
} | |
private static Random rand = new Random(); | |
private static List<KVPair> generateRandom(int size) { | |
List<KVPair> list = new ArrayList<KVPair>(size); | |
for (int i = 0; i < size; i++) { | |
String k = String.valueOf(rand.nextInt()); | |
//limit value size to [0, 1000) to make sure list contains duplicates | |
String v = String.valueOf(rand.nextInt(1000)); | |
list.add(new KVPair(k, v)); | |
} | |
return list; | |
} | |
private static Set<String> u1(List<KVPair> pairs) { | |
Set<String> undefined = new HashSet<String>(); | |
for (KVPair pair : pairs) { | |
undefined.add(pair.getValue()); | |
} | |
if (undefined.size() == 1) { | |
return new HashSet<String>(); | |
} | |
return undefined; | |
} | |
private static List<String> u2(List<KVPair> pairs) { | |
List<String> undefined = new ArrayList<String>(); | |
for (KVPair pair : pairs) { | |
if (!undefined.contains(pair.getValue())) { | |
undefined.add(pair.getValue()); | |
} | |
} | |
return undefined; | |
} | |
private static List<String> u3(List<KVPair> pairs) { | |
List<String> undefined = new LinkedList<String>(); | |
Iterator<KVPair> it = pairs.iterator(); | |
while (it.hasNext()) { | |
String value = it.next().getValue(); | |
if (!undefined.contains(value)) { | |
undefined.add(value); | |
} | |
} | |
return undefined; | |
} | |
} |
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
Starting benchmark of u1 | |
10000 iterations took 2203 ms | |
Mean iteration time 0.220 ms | |
Starting benchmark of u2 | |
10000 iterations took 112173 ms | |
Mean iteration time 11.217 ms | |
Starting benchmark of u3 | |
10000 iterations took 127456 ms | |
Mean iteration time 12.746 ms | |
Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment