forked from malteschmitz/slideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.coffee
48 lines (36 loc) · 1.14 KB
/
performance.coffee
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
{timestampPointers} = require "./basic"
linearSearch = (current, array) ->
for value in array
break if value[0] > current
nextElement = value
nextElement
binarySearch = (current, array) ->
minIndex = 0
maxIndex = array.length - 1
loop
if minIndex > maxIndex
return nextElement
currentIndex = (minIndex + maxIndex) / 2 | 0
value = array[currentIndex]
if value[0] <= current
nextElement = value
minIndex = currentIndex + 1
else
maxIndex = currentIndex - 1
for timestamp in [1..3600]
lin = linearSearch(timestamp, timestampPointers)
bin = binarySearch(timestamp, timestampPointers)
if lin != bin
console.log "#{lin} != #{bin} for #{timestamp}"
start = process.hrtime()
for i in [1..10000]
for timestamp in [1..3600]
linearSearch(timestamp, timestampPointers)
t = process.hrtime(start)
console.log('linear search took %d seconds', t[0] + t[1] / 1000000000);
start = process.hrtime()
for i in [1..10000]
for timestamp in [1..3600]
binarySearch(timestamp, timestampPointers)
t = process.hrtime(start)
console.log('binary search took %d seconds', t[0] + t[1] / 1000000000);