
To the Collider!
Testo is a new PHP testing framework that lets you benchmark code with a single attribute — no extra tools, no boilerplate. 🤔 The Problem Write a function that calculates the sum of all numbers from $a to $b . For example, if $a = 1 and $b = 5 , the result is 1 + 2 + 3 + 4 + 5 = 15 . The simplest solution that comes to mind: iteratively add $i to $a in a for loop until we reach $b , but that's too obvious. Imagination takes over and you want to solve it with arrays: fill an array with values from $a to $b and pass it to sum() . Comparing the Solutions PHP performs very well in synthetic benchmarks, outpacing Python and all that. With JIT enabled and a few tricks, it can even catch up to C++. We won't be comparing PHP with other languages right now — instead, let's just compare these two approaches against each other. Here's my reasoning: The array solution should be slower than the for loop, since extra resources go into computing hashes for the hash table when creating the array, and
Continue reading on Dev.to
Opens in a new tab


