Download Jar. Libraryless. Click here for Pure Java version (1069L/8K).
!7 static int n = 1000000; // iterate safely (& quickly) in the face of concurrent modifications static <A, B> IterableIterator<A> iterateKeysQuickly(final NavigableMap<A, B> map) { ret iteratorFromFunction(new F0<A>() { Iterator<A> it = keys(map).iterator(); A key; A get() { synchronized(map) { try { if (!it.hasNext()) null; ret key = it.next(); } catch (ConcurrentModificationException e) { print("Re-iterating"); it = map.tailMap(key, false).keySet().iterator(); if (!it.hasNext()) null; ret key = it.next(); // Can't throw another exception } } } }); } p { final NavigableMap<S, Bool> map = synchroNavigableMap(litcimap()); final new L<S> ids; repeat n { map.put(addAndReturn(ids, randomID()), Bool.TRUE); } repeat 5 { int i = 0; long sum = 0; time "Iterating" { for (S key : keys(map)) { if (!key.startsWith("_")) { ++i; sum += key.hashCode(); } } } assertEquals(i, n); i = 0; long sum2 = 0; time "Iterating quickly" { for (S key : iterateKeysQuickly(map)) { if (!key.startsWith("_")) { ++i; sum2 += key.hashCode(); } } } assertEquals(i, n); assertEquals(sum, sum2); } }
Began life as a copy of #1012166
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1012169 |
Snippet name: | Bench concurrency-safely iterating TreeMap faster [OK, just slightly slower than direct iteration] |
Eternal ID of this version: | #1012169/23 |
Text MD5: | 3a00171210f8a8a746a73c71774d8d20 |
Transpilation MD5: | 01a03cd2644cc0dd948b4b22c64c3822 |
Author: | stefan |
Category: | javax / collections |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-11-23 11:16:54 |
Source code size: | 1420 bytes / 60 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 498 / 1179 |
Version history: | 22 change(s) |
Referenced in: | [show references] |