-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart2.html
245 lines (226 loc) · 8.06 KB
/
chart2.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Language Speed Test</title>
<style>
body, h1, h2, h3, button, input {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f0f4f8;
color: #333;
padding: 20px;
}
h1 {
font-size: 2rem;
text-align: center;
color: #4a90e2;
margin-bottom: 20px;
}
label {
font-size: 1rem;
display: block;
margin-bottom: 10px;
}
input {
padding: 12px;
border-radius: 5px;
border: 1px solid #ddd;
width: 100%;
margin-bottom: 20px;
font-size: 1rem;
}
button {
padding: 12px 20px;
font-size: 1rem;
border-radius: 5px;
border: none;
background-color: #4a90e2;
color: white;
cursor: pointer;
margin: 5px 0;
}
button:hover {
background-color: #357abd;
}
.results {
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 100%;
max-width: 400px;
margin: 10px auto;
}
pre {
background-color: #e8eef4;
padding: 15px;
border-radius: 5px;
overflow: auto;
max-height: 150px;
}
#winnerOutput {
text-align: center;
font-size: 1.2rem;
color: #28a745;
margin-top: 20px;
}
#chartContainer {
max-width: 800px;
margin: 30px auto;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<h1>Language Speed Test</h1>
<label for="countInput">Enter number of prints (1 - 1,000,000):</label>
<input type="number" id="countInput" placeholder="Enter a number" min="1" max="1000000">
<button id="startBtn">Start Test</button>
<button id="stopBtn">Stop Test</button>
<div class="results">
<h3>Results:</h3>
<pre id="output"></pre>
</div>
<div id="winnerOutput"></div>
<div id="chartContainer">
<canvas id="speedChart"></canvas>
</div>
<script>
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const output = document.getElementById('output');
const winnerOutput = document.getElementById('winnerOutput');
const countInput = document.getElementById('countInput');
let intervals = {};
let times = {};
let running = false;
let targetCount = 0;
let completedTimes = {};
// Animation variables
let animationProgress = 0;
let animationFrame = null;
const ctx = document.getElementById('speedChart').getContext('2d');
const speedChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Python', 'C++', 'C#', 'Java', 'JavaScript', 'Ruby'],
datasets: [{
label: 'Execution Time (seconds)',
backgroundColor: ['#4a90e2', '#e74c3c', '#2ecc71', '#f39c12', '#9b59b6', '#3498db'],
data: Array(6).fill(0)
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Time (seconds)'
}
}
}
}
});
startBtn.onclick = () => {
reset();
const inputNumber = parseInt(countInput.value, 10);
if (isNaN(inputNumber) || inputNumber < 1 || inputNumber > 1000000) {
alert("Please enter a number between 1 and 1,000,000.");
return;
}
targetCount = inputNumber;
running = true;
startTests();
animateChart(); // Start the animation
};
function startTests() {
['python', 'cpp', 'csharp', 'java', 'javascript', 'ruby'].forEach((lang, index) => {
times[lang] = Date.now();
completedTimes[lang] = 0;
intervals[lang] = setInterval(() => {
if (running) {
updateOutput(lang, index);
}
}, 10);
});
}
function updateOutput(lang, index) {
const completedCount = completedTimes[lang] + 1;
completedTimes[lang] = completedCount;
if (completedCount <= targetCount) {
output.textContent = `${lang.toUpperCase()}: Printed ${completedCount} numbers`;
updateChart(lang, completedCount, index); // Update chart on each count
} else {
clearInterval(intervals[lang]);
const time = ((Date.now() - times[lang]) / 1000).toFixed(2);
output.textContent += `\n${lang.toUpperCase()}: Completed printing ${targetCount} numbers in ${time} seconds.`;
updateChart(lang, time, index);
}
}
function updateChart(lang, countOrTime, index) {
let time = countOrTime / 1000; // Time in seconds
if (countOrTime > targetCount) { // If test is completed, use the actual time
time = parseFloat(countOrTime);
}
speedChart.data.datasets[0].data[index] = time;
speedChart.update();
determineWinner();
}
function determineWinner() {
const times = speedChart.data.datasets[0].data;
const minTime = Math.min(...times.filter(time => time > 0));
const winningIndex = times.indexOf(minTime);
if (winningIndex !== -1) {
winnerOutput.textContent = `Winner: ${speedChart.data.labels[winningIndex]} with ${minTime} seconds.`;
}
}
function animateChart() {
animationProgress = 0;
animationFrame = requestAnimationFrame(() => {
// Calculate animated time based on progress
const animatedTime = animationProgress / 1000;
// Update the chart only with the animated time
speedChart.data.datasets[0].data.forEach((_, index) => {
speedChart.data.datasets[0].data[index] = animatedTime;
});
speedChart.update();
animationProgress += 10;
if (animationProgress <= targetCount) {
animateChart(); // Continue animation
} else {
// Stop animation and display actual times
speedChart.data.datasets[0].data.forEach((time, index) => {
speedChart.data.datasets[0].data[index] = time / 1000; // Use actual time
});
speedChart.update();
}
});
}
function reset() {
running = false;
animationProgress = 0;
cancelAnimationFrame(animationFrame);
Object.values(intervals).forEach(clearInterval);
intervals = {};
output.textContent = '';
winnerOutput.textContent = '';
speedChart.data.datasets[0].data.fill(0);
speedChart.update();
completedTimes = {};
}
stopBtn.onclick = () => {
running = false;
cancelAnimationFrame(animationFrame);
Object.values(intervals).forEach(clearInterval);
};
</script>
</body>
</html>