| I wanted to play with variations of the code. For that it is useful to make it output a "summary" so you know the variation you tried is computationally equivalent. For the first benchmark, I added a combined string length calcuclation: def main():
r = 0
for j in range(20):
for i in range(1000000):
r += len(str(i))
print(r)
main()
When I execute it: time python3 test.py
I get 8.3s execution time.The PHP equivalent: <?php
function main() {
$r = 0;
for ($j=0;$j<20;$j++)
for ($i=0;$i<1000000;$i++)
$r += strlen($i);
print("$r\n");
}
main();
When I execute it: time php test.php
Finishes in 1.4s here. So about 6x faster.Executing the Python version via PyPy: time pypy test.py
Gives me 0.49s. Wow!For better control, I did all runs inside a Docker container. Outside the container, all runs are about 20% faster. Which I also find interesting. Would like to see how the code performs in some more languages like Javascript, Ruby and Java. |