I got very little speed-up on pypy from being in a function (0.043 vs 0.033), but I got a 1.8X speed-up for py2 vs py3 (0.7686/.4322 = 1.78). (All programs emit 4999874750 as expected.)
EDIT: This uses a Nim program https://github.com/c-blake/bu/blob/main/doc/ru.md run as `ru -t`, but for the very fast variants you can get a more precise wall time from https://github.com/c-blake/bu/blob/main/doc/tim.md
pypy3_10-7.3.16_p1 - (first form,2nd form) x same in a func
TM 0.045421363 wall 0.034517 usr 0.010855 sys 99.9 % 56476 mxRS minCall
TM 0.043447211 wall 0.029492 usr 0.013763 sys 99.6 % 56124 mxRS ifStmt
TM 0.033408064 wall 0.020605 usr 0.012758 sys 99.9 % 55960 mxRS minCallInFunc
TM 0.033115636 wall 0.026059 usr 0.007018 sys 99.9 % 55956 mxRS ifStmtInFunc
python-2.7.18_p16
TM 1.755861105 wall 1.751740 usr 0.001991 sys 99.9 % 5944 mxRS
TM 0.940162912 wall 0.935362 usr 0.003988 sys 99.9 % 5916 mxRS
TM 1.155755936 wall 1.151529 usr 0.002993 sys 99.9 % 5928 mxRS
TM 0.432215672 wall 0.430851 usr 0.000998 sys 99.9 % 5800 mxRS
python-3.11.9_p1
TM 2.678891504 wall 2.675141 usr 0.000994 sys 99.9 % 7992 mxRS
TM 1.348009364 wall 1.344612 usr 0.001995 sys 99.9 % 7864 mxRS
TM 2.018986091 wall 2.014702 usr 0.001994 sys 99.9 % 7972 mxRS
TM 0.768633122 wall 0.766915 usr 0.000997 sys 99.9 % 7980 mxRS
I recall when Python3 was first going through its growing pains in the mid noughties that promises of eventually clawing back performance. This clawback now seems to have been a fantasy (or perhaps they have just piled on so many new features that they clawed back and then regressed?).
Anyway, Nim is even faster than PyPy and uses less memory than either version of CPython:
doIt.nim:
proc doIt() =
var i = 10000000
var r = 0
while i > 0:
i = i - 1
r += min(i, 500)
echo r
doIt()
#TM 0.024735 wall 0.024743 usr 0.000000 sys 100.0% 1.504 mxRM
In terms of "how Python-like is Nim", all I did was change `def` to `proc` and add the 2 `var`s and change `print` -> `echo`. { EDIT: though if you love Py print(), there is always
https://github.com/c-blake/cligen/blob/master/cligen/print.n... or some other roll-your-own idea. Then instead of py2/py3 print x,y vs print(x,y) you can actually do
either one in Nim since its call syntax is so flexible. }
It is perhaps noteworthy that, if the values are realized in some kind of array rather than generated by a loop, that modern CPUs can do this kind of calculation well with SIMD and compilers like gcc can even recognize the constructs and auto-vectorize. Of course, needing to load data costs memory bandwidth which may not be great compared to SIMD instruction throughput at scales past 80 MiB as in this problem.