Skip to content

Instantly share code, notes, and snippets.

View PierreQuentel's full-sized avatar

Pierre Quentel PierreQuentel

View GitHub Profile
@PierreQuentel
PierreQuentel / Benchmark CPython Brython Skulpt pypy.jsloop on list
Created April 7, 2015 08:29
Benchmark speed of CPython and Python-in-the-browser implementations : Brython, Skulpt, pypy.js. Uses a loop on a list.
import time
t = ['a']*1000
t0 = time.time()
for x in t:
for y in t:
a = 1
print("assignment.py", time.time()-t0)
@PierreQuentel
PierreQuentel / One-liner for pypy.js console
Created April 7, 2015 08:31
Benchmark speed of CPython and Python-in-the-browser implementations : Brython, Skulpt, pypy.js. Uses a loop on a list. One-liner for pypy.js console
'''import time\n\nt = ['a']*1000\n\nt0 = time.time()\nfor x in t:\n for y in t:\n a = 1\nprint("assignment.py", time.time()-t0)\n\nt0 = time.time()\na = 0\nfor x in t:\n for y in t:\n a += 1\nprint("augm_assign.py", time.time()-t0)\n\nt0 = time.time()\nfor x in t:\n for y in t:\n a = 1.0\nprint("assignment_float.py", time.time()-t0)\n\nt0 = time.time()\nfor x in t:\n for y in t:\n a = {0:0}\nprint("build_dict.py", time.time()-t0)\n\nt0 = time.time()\na = {0:0}\n\nfor x in t:\n for y in t:\n a[0] = y\n\nprint("set_dict_item.py", time.time()-t0)\n\nt0 = time.time()\nfor x in t:\n for y in t:\n a = [1, 2, 3]\nprint("build_list.py", time.time()-t0)\n\nt0 = time.time()\na = [0]\n\nfor x in t:\n for y in t:\n a[0] = y\nprint("set_list_item.py", time.time()-t0)\n\nt0 = time.time()\na, b, c = 1, 2, 3\nfor x in t:\n for y in t:\n a + b + c\nprint("add_integers.py", time.time()-t0)\n\nt0 = time.time()\na, b, c = 'a', 'b', 'c'\nfor x in t:\n
@PierreQuentel
PierreQuentel / gist:c2286b5795d920e5262b
Created April 7, 2015 08:33
Benchmark speed of CPython and Python-in-the-browser implementations : Brython, Skulpt, pypy.js. Uses xrange for Skulpt instead of range
import time
t0 = time.time()
for i in xrange(1000000):
a = 1
print("assignment.py", time.time()-t0)
t0 = time.time()
a = 0
for i in xrange(1000000):
@PierreQuentel
PierreQuentel / gist:9507d182bce901a33d1b
Created April 7, 2015 08:34
Benchmark speed of CPython and Python-in-the-browser implementations : Brython, Skulpt, pypy.js. One-liner for for pypy.js, uses xrange instead of range
'''import time\n\nt0 = time.time()\nfor i in xrange(1000000):\n a = 1\nprint("assignment.py", time.time()-t0)\n\nt0 = time.time()\na = 0\nfor i in xrange(1000000):\n a += 1\nprint("augm_assign.py", time.time()-t0)\n\nt0 = time.time()\nfor i in xrange(1000000):\n a = 1.0\nprint("assignment_float.py", time.time()-t0)\n\nt0 = time.time()\nfor i in xrange(1000000):\n a = {0:0}\nprint("build_dict.py", time.time()-t0)\n\nt0 = time.time()\na = {0:0}\n\nfor i in xrange(1000000):\n a[0] = i\n\nassert a[0]==999999\nprint("set_dict_item.py", time.time()-t0)\n\nt0 = time.time()\nfor i in xrange(1000000):\n a = [1, 2, 3]\nprint("build_list.py", time.time()-t0)\n\nt0 = time.time()\na = [0]\n\nfor i in xrange(1000000):\n a[0] = i\nprint("set_list_item.py", time.time()-t0)\n\nt0 = time.time()\na, b, c = 1, 2, 3\nfor i in xrange(1000000):\n a + b + c\nprint("add_integers.py", time.time()-t0)\n\nt0 = time.time()\na, b, c = 'a', 'b', 'c'\nfor i in xrange(1000000):\n a + b + c\nprint("add_strings.py",
@PierreQuentel
PierreQuentel / gist:a41aced697b2f53c1aa5
Created April 7, 2015 19:03
Results with xrange for pypy.js and skulpt
With xrange for pypy.js and skulpt
Code for Skulpt : https://gist.github.com/PierreQuentel/c2286b5795d920e5262b
Code for pypy.js : https://gist.github.com/PierreQuentel/9507d182bce901a33d1b
| x slower than Cpython
Cpython Brython pypy.js skulpt | Brython pypy.js skulpt
assignment 125 14 3245 5936 | 0.11 26.05 47.64
augm_assign 211 44 4105 6659 | 0.21 19.49 31.61
assignment_float 110 508 3384 6027 | 4.63 30.85 54.94
@PierreQuentel
PierreQuentel / gist:dfb9e1c3e60046ebc288
Created April 7, 2015 19:08
Benchmark with a loop on lists of strings instead of range()
Code for CPython, Brython and Skulpt : https://gist.github.com/PierreQuentel/50047f7d15b2e7d41516
Code for pypy.js : https://gist.github.com/PierreQuentel/d9b2c037089cd7361d42
| x slower than Cpython
Cpython Brython pypy.js skulpt | Brython pypy.js skulpt
assignment 112 1175 3961 5494 | 10.52 35.45 49.18
augm_assign 185 1744 4721 6533 | 9.42 25.51 35.30
assignment_float 117 2244 4112 5795 | 19.23 35.23 49.65
build_dict 369 7582 4398 13805 | 20.53 11.91 37.38
set_dict_item 188 1495 5492 22455 | 7.95 29.20 119.40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="http://rawgit.com/brython-dev/brython/master/www/src/brython.js"></script>
</head>
<body onload="brython(2);test()">
@PierreQuentel
PierreQuentel / parse imports
Created October 19, 2016 20:34
Sample code to list all imports in a script
import ast
src = """import X
from Y import (
A, b)
def f():
pass
"""
@PierreQuentel
PierreQuentel / .py
Created November 18, 2020 16:53
Alternative version of turtle.py without importing copy and inspect
# A revised version of CPython's turtle module written for Brython
#
# Note: This version is not intended to be used in interactive mode,
# nor use help() to look up methods/functions definitions. The docstrings
# have thus been shortened considerably as compared with the CPython's version.
#
# All public methods/functions of the CPython version should exist, if only
# to print out a warning that they are not implemented. The intent is to make
# it easier to "port" any existing turtle program from CPython to the browser.
@PierreQuentel
PierreQuentel / finder.py
Created December 29, 2020 18:13
PathFinder example (cf. issue #1182 in brython-dev/brython)
import sys
from browser import ajax
req = ajax.Ajax()
class Finder:
"""Meta path finder, searches Python modules or packages in the
application directory."""
@classmethod