-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwise.html
127 lines (110 loc) · 4.22 KB
/
bitwise.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bitwise operations</title>
</head>
<body>
<script>
// Setup
let arr = [];
function setupNumbers() {
arr = [];
for (let i=0; i < 10000000; i++) {
arr.push(Math.floor(Math.random()*10000));
}
}
let output = document.body;
function runDivision(divider) {
let arr1 = [...arr];
let timeStart = new Date();
Divide(arr1, divider);
let timeEnd = new Date();
customLog("Run time: ", timeEnd-timeStart);
customLog("first 5 results: ", arr1[0], " ", arr1[1], " ", arr1[2], " ", arr1[3], " ", arr1[4], "...");
return timeEnd-timeStart;
}
function runShift(shiftBy) {
// Setup
let arr2 = [...arr];
let timeStart = new Date();
BinaryShift(arr2, shiftBy);
let timeEnd = new Date();
customLog("Run time:", timeEnd-timeStart);
customLog("first 5 results: ", arr2[0], " ", arr2[1], " ", arr2[2], " ", arr2[3], " ", arr2[4], "...");
return timeEnd-timeStart;
}
function getAverageLoop(repeatCount, divider) {
let timeSumDivision = 0;
let timeSumShift = 0;
for(let i =0; i < repeatCount; i++) {
setupNumbers();
let arg = divider;
// Division
output = document.getElementById("outputDivision");
timeSumDivision = timeSumDivision + runDivision(arg);
// Shift
const bin = (divider).toString(2);
const shiftBy = bin.length - bin.lastIndexOf("1") - 1;
arg = shiftBy;
output = document.getElementById("outputShift");
timeSumShift = timeSumShift + runShift(arg);
}
output = document.getElementById("outputDivision");
customLog(`Average time of Division is ${timeSumDivision/repeatCount}ms`);
output = document.getElementById("outputShift");
customLog(`Average time of Binary Shift is ${timeSumShift/repeatCount}ms`);
}
function customLog() {
console.log(...arguments);
for (const arg of arguments) {
let sp = document.createElement("span");
sp.innerText = arg.toString();
output.appendChild(sp);
}
let br = document.createElement("br");
output.appendChild(br);
let hr = document.createElement("hr");
output.appendChild(hr);
}
// --------------------------------------------------------------------------------------
function Divide(numbers, divider) {
const end = numbers.length;
for (let i=0; i < end; i++) {
numbers[i] = Math.floor(numbers[i] / divider);
}
}
function BinaryShift(numbers, shiftBy) {
const end = numbers.length;
for (let i=0; i < end; i++) {
numbers[i] = numbers[i] >> shiftBy;
}
}
// --------------------------------------------------------------------------------------
</script>
<div>
<h1>Division vs. Binary shift</h1>
<p>Example: 10 000 000 random numbers, integer division by multiples of 2</p>
<h2>Division</h2>
<pre>Math.floor(number / 2);</pre>
<h2>Binary shift</h2>
<pre>number >> 1</pre>
<p>Number of repetitions: <input type="number" value="8" id="loopCount"></p>
<table id="results">
<tr>
<td>
<div id="outputDivision"></div>
</td>
<td>
<div id="outputShift"></div>
</td>
</tr>
<tr>
<td colspan="2">
<button onclick="getAverageLoop(document.getElementById('loopCount').value, 2)">Benchmark Division</button>
</td>
</tr>
</table>
</div>
</body>
</html>