sets.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import * as B from 'benchmark'
  2. import Tuple from '../mol-base/collections/integer/tuple'
  3. import OrdSet from '../mol-base/collections/integer/ordered-set'
  4. import AtomSet from '../mol-data/atom-set'
  5. import Segmentation from '../mol-base/collections/integer/segmentation'
  6. export namespace Iteration {
  7. const U = 1000, V = 2500;
  8. const control: number[] = [];
  9. const sets = Object.create(null);
  10. for (let i = 1; i < U; i++) {
  11. const set = [];
  12. for (let j = 1; j < V; j++) {
  13. control[control.length] = j * j + 1;
  14. set[set.length] = j * j + 1;
  15. }
  16. sets[i * i] = OrdSet.ofSortedArray(set);
  17. }
  18. const ms = AtomSet.create(sets);
  19. export function native() {
  20. let s = 0;
  21. for (let i = 0, _i = control.length; i < _i; i++) s += control[i];
  22. return s;
  23. }
  24. export function iterators() {
  25. let s = 0;
  26. const it = AtomSet.atoms(ms);
  27. while (it.hasNext) {
  28. const v = it.move();
  29. s += Tuple.snd(v);
  30. }
  31. return s;
  32. }
  33. export function elementAt() {
  34. let s = 0;
  35. for (let i = 0, _i = AtomSet.atomCount(ms); i < _i; i++) s += Tuple.snd(AtomSet.atomGetAt(ms, i));
  36. return s;
  37. }
  38. export function manual() {
  39. let s = 0;
  40. const keys = AtomSet.unitIds(ms);
  41. for (let i = 0, _i = OrdSet.size(keys); i < _i; i++) {
  42. const set = AtomSet.unitGetById(ms, OrdSet.getAt(keys, i));
  43. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  44. s += OrdSet.getAt(set, j);
  45. }
  46. }
  47. return s;
  48. }
  49. export function manual1() {
  50. let s = 0;
  51. for (let i = 0, _i = AtomSet.unitCount(ms); i < _i; i++) {
  52. const set = AtomSet.unitGetByIndex(ms, i);
  53. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  54. s += OrdSet.getAt(set, j);
  55. }
  56. }
  57. return s;
  58. }
  59. export function run() {
  60. const suite = new B.Suite();
  61. // const values: number[] = [];
  62. // for (let i = 0; i < 1000000; i++) values[i] = (Math.random() * 1000) | 0;
  63. console.log(Iteration.native(), Iteration.iterators(), Iteration.elementAt(), Iteration.manual(), Iteration.manual1());
  64. suite
  65. .add('native', () => Iteration.native())
  66. .add('iterators', () => Iteration.iterators())
  67. .add('manual', () => Iteration.manual())
  68. .add('manual1', () => Iteration.manual1())
  69. .add('el at', () => Iteration.elementAt())
  70. .on('cycle', (e: any) => console.log(String(e.target)))
  71. .run();
  72. }
  73. }
  74. export namespace Union {
  75. function createArray(n: number) {
  76. const data = new Int32Array(n);
  77. let c = (Math.random() * 100) | 0;
  78. for (let i = 0; i < n; i++) {
  79. data[i] = c;
  80. c += 1 + (Math.random() * 100) | 0
  81. }
  82. return data;
  83. }
  84. function rangeArray(o: number, n: number) {
  85. const data = new Int32Array(n);
  86. for (let i = 0; i < n; i++) {
  87. data[i] = o + i;
  88. }
  89. return data;
  90. }
  91. function createData(array: ArrayLike<number>) {
  92. const obj = Object.create(null);
  93. const set = new Set<number>();
  94. for (let i = 0; i < array.length; i++) {
  95. const a = array[i];
  96. obj[a] = 1;
  97. set.add(a);
  98. }
  99. return { ordSet: OrdSet.ofSortedArray(array), obj, set }
  100. }
  101. function unionOS(a: OrdSet, b: OrdSet) {
  102. return OrdSet.union(a, b);
  103. }
  104. function intOS(a: OrdSet, b: OrdSet) {
  105. return OrdSet.intersect(a, b);
  106. }
  107. function unionO(a: object, b: object) {
  108. const ret = Object.create(null);
  109. for (const k of Object.keys(a)) ret[k] = 1;
  110. for (const k of Object.keys(b)) ret[k] = 1;
  111. return ret;
  112. }
  113. function intO(a: object, b: object) {
  114. const ret = Object.create(null);
  115. for (const k of Object.keys(a)) if ((b as any)[k]) ret[k] = 1;
  116. return ret;
  117. }
  118. function _setAdd(this: Set<number>, x: number) { this.add(x) }
  119. function unionS(a: Set<number>, b: Set<number>) {
  120. const ret = new Set<number>();
  121. a.forEach(_setAdd, ret);
  122. b.forEach(_setAdd, ret);
  123. return ret;
  124. }
  125. function _setInt(this: { set: Set<number>, other: Set<number> }, x: number) { if (this.other.has(x)) this.set.add(x) }
  126. function intS(a: Set<number>, b: Set<number>) {
  127. if (a.size < b.size) {
  128. const ctx = { set: new Set<number>(), other: b };
  129. a.forEach(_setInt, ctx);
  130. return ctx.set;
  131. } else {
  132. const ctx = { set: new Set<number>(), other: a };
  133. b.forEach(_setInt, ctx);
  134. return ctx.set;
  135. }
  136. }
  137. export function run() {
  138. const suite = new B.Suite();
  139. const { ordSet: osA, set: sA, obj: oA } = createData(createArray(1000));
  140. const { ordSet: osB, set: sB, obj: oB } = createData(createArray(1000));
  141. console.log(OrdSet.size(unionOS(osA, osB)), Object.keys(unionO(oA, oB)).length, unionS(sA, sB).size);
  142. console.log(OrdSet.size(intOS(osA, osB)), Object.keys(intO(oA, oB)).length, intS(sA, sB).size);
  143. suite
  144. .add('u ord set', () => unionOS(osA, osB))
  145. .add('u obj', () => unionO(oA, oB))
  146. .add('u ES6 set', () => unionS(sA, sB))
  147. .add('i ord set', () => intOS(osA, osB))
  148. .add('i obj', () => intO(oA, oB))
  149. .add('i ES6 set', () => intS(sA, sB))
  150. .on('cycle', (e: any) => console.log(String(e.target)))
  151. .run();
  152. }
  153. export function runR() {
  154. const suite = new B.Suite();
  155. const rangeA = rangeArray(145, 1000);
  156. const rangeB = rangeArray(456, 1000);
  157. const { ordSet: osA, set: sA, obj: oA } = createData(rangeA);
  158. const { ordSet: osB, set: sB, obj: oB } = createData(rangeB);
  159. console.log(OrdSet.size(unionOS(osA, osB)), Object.keys(unionO(oA, oB)).length, unionS(sA, sB).size);
  160. console.log(OrdSet.size(intOS(osA, osB)), Object.keys(intO(oA, oB)).length, intS(sA, sB).size);
  161. suite
  162. .add('u ord set', () => unionOS(osA, osB))
  163. .add('u obj', () => unionO(oA, oB))
  164. .add('u ES6 set', () => unionS(sA, sB))
  165. .add('i ord set', () => intOS(osA, osB))
  166. .add('i obj', () => intO(oA, oB))
  167. .add('i ES6 set', () => intS(sA, sB))
  168. .on('cycle', (e: any) => console.log(String(e.target)))
  169. .run();
  170. }
  171. }
  172. export namespace Build {
  173. function createSorted() {
  174. const b = AtomSet.SortedBuilder(AtomSet.Empty);
  175. for (let i = 0; i < 100; i++) {
  176. for (let j = 0; j < 100; j++) {
  177. b.add(i, j);
  178. }
  179. }
  180. return b.getSet();
  181. }
  182. function createByUnit() {
  183. const b = AtomSet.SortedBuilder(AtomSet.Empty);
  184. for (let i = 0; i < 100; i++) {
  185. b.beginUnit();
  186. for (let j = 0; j < 100; j++) {
  187. b.addToUnit(j);
  188. }
  189. b.commitUnit(i);
  190. }
  191. return b.getSet();
  192. }
  193. export function run() {
  194. const suite = new B.Suite();
  195. suite
  196. .add('create sorted', () => createSorted())
  197. .add('create by unit', () => createByUnit())
  198. .on('cycle', (e: any) => console.log(String(e.target)))
  199. .run();
  200. }
  201. }
  202. export namespace Tuples {
  203. function createData(n: number) {
  204. const ret: Tuple[] = new Float64Array(n) as any;
  205. for (let i = 0; i < n; i++) {
  206. ret[i] = Tuple.create(i, i * i + 1);
  207. }
  208. return ret;
  209. }
  210. function sum1(data: ArrayLike<Tuple>) {
  211. let s = 0;
  212. for (let i = 0, _i = data.length; i < _i; i++) {
  213. s += Tuple.fst(data[i]) + Tuple.snd(data[i]);
  214. }
  215. return s;
  216. }
  217. function sum2(data: ArrayLike<Tuple>) {
  218. let s = 0;
  219. for (let i = 0, _i = data.length; i < _i; i++) {
  220. const t = data[i];
  221. s += Tuple.fst(t) + Tuple.snd(t);
  222. }
  223. return s;
  224. }
  225. export function run() {
  226. const suite = new B.Suite();
  227. const data = createData(10000);
  228. suite
  229. .add('sum fst/snd', () => sum1(data))
  230. .add('sum fst/snd 1', () => sum2(data))
  231. .on('cycle', (e: any) => console.log(String(e.target)))
  232. .run();
  233. }
  234. }
  235. export function testSegments() {
  236. const data = OrdSet.ofSortedArray([4, 9, 10, 11, 14, 15, 16]);
  237. const segs = Segmentation.create([0, 4, 10, 12, 13, 15, 25]);
  238. const it = Segmentation.transientSegments(segs, data);
  239. while (it.hasNext) {
  240. const s = it.move();
  241. for (let j = s.start; j < s.end; j++) {
  242. console.log(`${s.index}: ${OrdSet.getAt(data, j)}`);
  243. }
  244. }
  245. }
  246. testSegments();
  247. //Tuples.run();
  248. // interface AA { kind: 'a' }
  249. // //interface BB { kind: 'b' }
  250. // interface AB { kind: 'a' | 'b' }
  251. // declare const a: AA;
  252. // export const ab: AB = a;