Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: tests/corelib/collection_length_test.dart

Issue 12391010: Make HashSet.length constant time. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/collection/splay_tree.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
Lasse Reichstein Nielsen 2013/02/28 11:00:50 Fixed 2013.
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library map_test;
6 import 'dart:collection';
7
8 // Test that length/isEmpty opertions are constant time on
9 // maps, strings and collections.
10
11 void testString(int n) {
12 String s = "x";
13 String string = "";
14 int length = n;
15 while (true) {
16 if ((length & 1) == 1) {
17 string = string.concat(s);
18 }
19 length >>= 1;
20 if (length == 0) break;
21 s = s.concat(s);
22 }
23 testLength(string, n);
24 testLength(string.codeUnits, n);
25 }
26
27 void testMap(Map map, int n) {
28 for (int i = 0; i < n; i++) {
29 map[i] = i;
30 }
31 testLength(map, n);
32 testLength(map.keys, n);
33 testLength(map.values, n);
34 }
35
36 void testCollection(Collection collection, n) {
37 for (int i = 0; i < n; i++) {
38 collection.add(i);
39 }
40 testLength(collection, n);
41 }
42
43 void testList(List list, n) {
44 for (int i = 0; i < n; i++) {
45 list[i] = i;
46 }
47 testLength(list, n);
48 }
49
50
51 void testLength(var lengthable, int size) {
52 print(lengthable.runtimeType); // Show what hangs the test.
53 int length = 0;
54 // If length or isEmpty is not a constant-time (or very fast) operation,
55 // this will timeout.
56 for (int i = 0; i < 100000; i++) {
57 if (!lengthable.isEmpty) length += lengthable.length;
58 }
59 if (length != size * 100000) throw "Bad length: $length / size: $size";
60 }
61
62
63 main() {
64 const int N = 100000;
65 testMap(new HashMap(), N);
66 testMap(new LinkedHashMap(), N);
67 testMap(new SplayTreeMap(), N);
68 testCollection(new HashSet(), N);
69 testCollection(new LinkedHashSet(), N);
70 testCollection(new ListQueue(), N);
71 testList(new List()..length = N, N);
72 testList(new List(N), N);
73 testString(N);
74 // DoubleLinkedQueue has linear length, but fast isEmpty.
75 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/splay_tree.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698