Skip to content

Instantly share code, notes, and snippets.

@fcamel
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcamel/beef85e5c3b9ab78507b to your computer and use it in GitHub Desktop.
Save fcamel/beef85e5c3b9ab78507b to your computer and use it in GitHub Desktop.
Benchmark CPython2, PyPy, Node.js
$ cat h.py
n = 100000;
d = {};
for _ in xrange(1000):
for k in xrange(n):
if k not in d:
d[k] = k * 2
else:
d[k] += 1
print d[n - 1]
$ time python h.py
200997
real 0m22.513s
user 0m22.499s
sys 0m0.008s
$ time pypy h.py
200997
real 0m5.091s
user 0m5.051s
sys 0m0.024s
$ cat h.js
n = 100000;
d = {};
for (i = 0; i < 1000; i++) {
for (k = 0; k < n; k++) {
if (!(k in d)) {
d[k] = k * 2;
} else {
d[k] += 1;
}
}
}
console.log(d[n - 1]);
$ time nodejs h.js
200997
real 0m2.939s
user 0m3.134s
sys 0m0.043s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment