-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresearch.js
162 lines (127 loc) · 3.67 KB
/
research.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const t = require('tape');
const cluster = require('./');
const random = require('gauss-random')
const bitsf64 = require('math-float64-from-bits')
const ui8bits = require('math-uint8-bits')
const f64bits = require('math-float64-bits')
t('packing 1e6 array', t => {
let y = new Float64Array(1e6)
let x = new Float64Array(1e6)
console.time(1)
y.subarray(0e5, 1e5)
y.subarray(1e5, 2e5)
y.subarray(2e5, 3e5)
y.subarray(3e5, 4e5)
y.subarray(4e5, 5e5)
y.subarray(5e5, 6e5)
y.subarray(6e5, 7e5)
y.subarray(7e5, 8e5)
y.subarray(8e5, 9e5)
console.timeEnd(1)
t.end()
})
t('float64 packing', t => {
// uint8 (level) + uint32 (id) + float32→normalized uint16 (x)
let f64 = new Float64Array(1)
let view = new DataView(f64.buffer)
let ui8 = new Uint8Array(f64.buffer)
let ui16 = new Uint16Array(f64.buffer)
let ui32 = new Uint32Array(f64.buffer)
// view.setUint8(7, parseInt('10000000', 2))
// view.setUint8(6, parseInt('00000000', 2))
// ui8[7] = 0xff
// ui16[2] = 0x1000
ui32[1] = 0x00ff0000 & 0x01 << 16 | 0x0000ffff & 0xffff
console.log(f64[0], f64bits(f64[0]))
let sign = '0' // ignore sign
let exp = '00000000000' // write levels as exponent
let fract = '0000000000000000000000000000000000000000000000000000'
console.log(bitsf64(sign + exp + fract))
// 4 '0100000000010000000000000000000000000000000000000000000000000000'
// -0 '1000000000000000000000000000000000000000000000000000000000000000'
// NaN '0111111111111000000000000000000000000000000000000000000000000000'
// +Inf '0111111111110000000000000000000000000000000000000000000000000000'
// -Inf '1111111111110000000000000000000000000000000000000000000000000000'
})
t('sorting comparison', t => {
let N = 1e6
let f64 = data(new Float64Array(N))
console.time(1)
f64.sort()
console.timeEnd(1)
let f32 = data(new Float32Array(N))
console.time(2)
f32.sort()
console.timeEnd(2)
let i32 = data(new Uint32Array(N), i => Math.random() * 0xffffffff)
console.time(3)
i32.sort()
console.timeEnd(3)
let i32b = data(new Uint32Array(N), i => Math.random() * 0xffffffff)
console.time(4)
i32b.sort((a, b) => a - b)
console.timeEnd(4)
let points = data(new Float64Array(N))
let levels = new Uint32Array(N)
let weights = new Uint32Array(N)
let ids = new Uint32Array(N)
let level = 0, i = 0
while (i < N) {
let amt = Math.floor(Math.random() * (N / 16))
let end = Math.min(i + amt, N)
for (; i < end; i++) {
levels[i] = level
ids[i] = i
}
level++
}
const snapSort = require('snap-points-2d/lib/sort')
console.time('snapsort')
snapSort(levels, points, ids, weights, N)
console.timeEnd('snapsort')
})
function data(N=1e6, f) {
let points
if (N.length) {
points = N
}
else {
points = Array(N)
}
for (let i = 0; i < points.length; i++) {
points[i] = f ? f(i) : random()
}
return points
}
/*
// use x-sort if required
if (options.sort) {
// pack levels: uint8, x-coord: uint16 and id: uint32 to float64
let packed = new Float64Array(n)
let packedInt = new Uint32Array(packed.buffer)
for (let i = 0; i < n; i++) {
packedInt[i * 2] = i
packedInt[i * 2 + 1] = (0x3ff00000 & (levels[i] << 20) | 0x0000ffff & ((1 - points[i * 2]) * 0xffff))
}
// do native sort
packed.sort()
// unpack data back
let sortedLevels = new Uint8Array(n)
let sortedWeights = new Uint32Array(n)
let sortedIds = new Uint32Array(n)
let sortedPoints = new Float64Array(n * 2)
for (let i = 0; i < n; i++) {
let id = packedInt[(n - i - 1) * 2]
sortedLevels[i] = levels[id]
sortedWeights[i] = weights[id]
sortedIds[i] = ids[id]
sortedPoints[i * 2] = points[id * 2]
sortedPoints[i * 2 + 1] = points[id * 2 + 1]
}
ids = sortedIds
levels = sortedLevels
points = sortedPoints
weights = sortedWeights
}
*/