Task 4 of 4
Sixteen numbers, doubled. The GPU can do it — but every kernel call pays a fixed toll before any math happens: dispatch through the graphics API, upload 16 values, read 16 back. A plain JavaScript loop finishes the whole job in nanoseconds, before the GPU has cleared its throat.
This is the module's payoff — the full honest-measurement checklist in one run: warm up first (task 1), remember the transfer toll (task 2), compare results with a tolerance (task 3), and then declare the true winner — even when it isn't the GPU. Parallel hardware pays off on big workloads; on tiny ones, the honest answer is a for-loop.
tiny both ways — kernel and plain loop —
verify they agree within a tolerance, time both fairly, and log the winner.data[this.thread.x] * 2 for all 16 threadsfromKernel to fromLoop element-wise with tolerance 1e-4 and log match: truems/roundwinner: with whichever contender was fasterTask 3's move, in a loop: start with let allMatch = true; and flip
it to false whenever Math.abs(fromKernel[i] - fromLoop[i]) > 1e-4.
The first doubleTiny(tiny) call already warmed the kernel up, so
both timed loops measure steady state. Time 200 rounds of doubleTiny(tiny),
then 200 rounds of the JS loop, and divide each total by 200.
console.log('winner:', kernelMs < loopMs ? 'gpu kernel' : 'plain js');
On a job this small, expect the loop to take it. That's the honest answer.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.