-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
124 lines (102 loc) · 2.5 KB
/
index.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
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<h2>Números narcisistas - WebWorker [Buffer]</h2>
<strong>N = </strong> <input id='nn1' type="number" value="7"> (Cuidado, el tiempo para ejecutarlo es exponencialmente proporcional a N)
<br><br>
Workers = <input id='nn2' type="number" value='4'>
<br><br>
<input id='nn3' type="button" value="Ejecutar"> <input id='nn5' type="button" value="Parar">
<br><br>
<pre id='nn4'></pre>
<script type="text/javascript">
function $(id){return (document.getElementById(id));}
var RES = [],
WKS = [],
buffer = [],
total = 0,
processed = 0,
run = false,
time;
function init(n,workers){
if(run){
alert('Actualmente en ejecución.');
return false;
}
finish();
var max = Math.pow(10,n),
part= max/workers;
RES = [];
WKS = [];
run = true;
total = processed = Number(workers);
time = (new Date()).getTime();
genBuffer(n);
for(var i=0;i<Number(workers);i++){
WKS[i] = new Worker('numNarcisita.js');
WKS[i].addEventListener('message',function(e){
if(e.data.status==0)
process();
else
add(e.data.n);
},false);
WKS[i].postMessage({
first: Math.floor(part*i),
last: Math.floor(part*(i+1))-1,
buffer: buffer
});
}
show();
update();
}
function genBuffer(n){
for(var a=0;a<=n;a++){
buffer[a] = [];
for(var b=0;b<10;b++) buffer[a][b] = Math.pow(b,a)
}
}
function add(n){
RES.push(n)
RES.sort(function(a,b){return a-b});
show();
}
function process(nums){
processed--;
show();
if(processed==0){
run = false;
finish();
}
}
function show(){
RES.sort(function(a,b){return a-b});
var text = ' ';
text += processed!=0?run?'<font color="#E00">Running</font>':'<font color="#00E">Stopped</font>':'<font color="#00E">Finished</font>';
text += ' ('+(total-processed)+'/'+total+')\n';
text += ' Time ';
text += ((new Date()).getTime()-time)/1000 + 's';
text += '\n-------------------------\n ';
for(var i=0;i<RES.length;i++){
text += RES[i]+'\n ';
}
$('nn4').innerHTML = text;
}
function update(){
if(run){
show();
factor = 250;
setTimeout(update,factor+Math.round(Math.random()*factor));
}
}
function finish(s){
if(!run) return false;
run = false;
for(var i=0;i<WKS.length;i++) WKS[i].terminate();
if(s) show();
}
$('nn3').addEventListener('click',function(){
init($('nn1').value,$('nn2').value);
},false);
$('nn5').addEventListener('click',function(){
finish(true);
},false);
</script>