Skip to content

Instantly share code, notes, and snippets.

@ibalashov
Created August 28, 2017 07:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ibalashov/0138e850e58942569a636dffa75f0bb9 to your computer and use it in GitHub Desktop.
Save ibalashov/0138e850e58942569a636dffa75f0bb9 to your computer and use it in GitHub Desktop.
List vs HashSet performance for small lists (single contains op)
public static List<Integer> generate(int series) {
return Stream.iterate(new int[]{0, 1}, s -> new int[]{s[1], s[0] + s[1]})
.limit(series)
.map(n -> n[0])
.collect(Collectors.toList());
}
public static void main(String[] args) {
generate(30).forEach(n -> {
List<String> collected = Stream.generate(() -> UUID.randomUUID().toString()).limit(n).collect(Collectors.toList());
String sample = UUID.randomUUID().toString();
Stopwatch sw1 = Stopwatch.createStarted();
collected.contains(sample);
long microList = sw1.elapsed(TimeUnit.MICROSECONDS);
HashSet<String> set = Sets.newHashSet(collected);
Stopwatch sw = Stopwatch.createStarted();
set.contains(sample);
long microSet = sw.elapsed(TimeUnit.MICROSECONDS);
System.out.printf("%d %d %d %n", n, microList, microSet);
});
0 26 12
1 2 0
1 1 0
2 1 0
3 1 0
5 2 1
8 2 1
13 3 0
21 5 1
34 7 1
55 8 1
89 13 1
144 17 1
233 33 1
377 701 1
610 68 1
987 111 2
1597 186 1
2584 1797 1
4181 566 2
6765 753 4
10946 1275 3
17711 1901 3
28657 2718 3
46368 2395 4
75025 1132 2
121393 1722 2
196418 2042 3
317811 4838 2
514229 6117 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment