WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.6% of users enabled 2FA.

perfcmp.py 4.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
"""
Performance comparison
"""
from time import time
from tempfile import mkdtemp
import py, os
import subprocess as subp
import ctypes

from rpython.translator.interactive import Translation
from rpython.config.translationoption import set_opt_level

13
from util import libext, preload_libmu
14 15 16 17

CPYTHON = os.environ.get('CPYTHON', 'python')
PYPY = os.environ.get('PYPY', 'pypy')
RPYTHON = os.environ.get('RPYTHON', None)
John Zhang's avatar
John Zhang committed
18
CC = os.environ.get('CC', 'clang')
19

20 21
perf_target_dir = py.path.local(__file__).join('perf_target')

22
def run(cmd):
23
    # print ' '.join(cmd)
24 25 26 27 28 29 30 31 32 33 34 35
    p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE)
    return p.communicate()

def get_c_function(lib, f):
    from ctypes import CDLL
    name = f.__name__
    return getattr(CDLL(lib.strpath), 'pypy_g_' + name)

def perf_fibonacci():
    tmpdir = py.path.local(mkdtemp())
    print tmpdir

36 37
    py_file = perf_target_dir.join('fibonacci.py')
    c_file = perf_target_dir.join('fibonacci.c')
38

39
    def run_cpython(N):
40 41 42
        out, _ = run([CPYTHON, py_file.strpath, str(N)])
        return float(out)

43
    def run_pypy_nojit(N):
44 45 46
        out, _ = run([PYPY, '--jit', 'off', py_file.strpath, str(N)])
        return float(out)

47 48
    def run_pypy(N):
        out, _ = run([PYPY, py_file.strpath, str(N)])
49 50 51
        return float(out)

    def compile_rpython_c():
52 53
        from perf_target.fibonacci import fib
        t = Translation(fib, [int],
54 55 56 57
                        gc='none')
        set_opt_level(t.config, '3')
        t.ensure_opt('gc', 'none')
        libpath = t.compile_c()
58
        fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + fib.__name__)
59 60 61
        return fnp

    def compile_rpython_c_jit():
62 63
        from perf_target.fibonacci import fib
        t = Translation(fib, [int],
64 65 66 67
                        gc='none')
        set_opt_level(t.config, 'jit')
        t.ensure_opt('gc', 'none')
        libpath = t.compile_c()
68
        fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + fib.__name__)
69 70
        return fnp

John Zhang's avatar
John Zhang committed
71 72 73 74 75 76
    def compile_c():
        libpath = tmpdir.join('libfibonacci' + libext)
        run([CC, '-fpic', '--shared', '-o', libpath.strpath, c_file.strpath])
        lib = ctypes.CDLL(libpath.strpath)
        return lib.fib

77
    def compile_rpython_mu():
78
        preload_libmu()
79

80 81
        from perf_target.fibonacci import fib
        t = Translation(fib, [int],
82 83 84 85 86 87 88 89
                        backend='mu', muimpl='fast', mucodegen='api', mutestjit=True)
        set_opt_level(t.config, '3')
        db, bdlgen, fnc_name = t.compile_mu()
        libname = 'lib%(fnc_name)s.dylib' % locals()
        bdlgen.mu.compile_to_sharedlib(libname, [])
        libpath = py.path.local().join('emit', libname)
        fnp = getattr(ctypes.CDLL(libpath.strpath), fnc_name)
        return fnp
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    def get_average_time(run_fnc, args, warmup=5, iterations=100):
        for i in range(warmup):
            run_fnc(*args)

        total = 0.0
        for i in range(iterations):
            total += run_fnc(*args)
        return total / iterations

    def get_average_time_compiled(compile_fnc, args, warmup=5, iterations=100):
        def run_funcptr(fnp, N):
            t0 = time()
            fnp(N)
            t1 = time()
            return t1 - t0

        fnp = compile_fnc()
        return get_average_time(lambda *a: run_funcptr(fnp, *a), args, warmup, iterations)

110 111
    N = 30
    iterations = 10
112 113 114 115 116 117 118

    t_cpython = get_average_time(run_cpython, [N], iterations=iterations)
    t_pypy_nojit = get_average_time(run_pypy_nojit, [N], iterations=iterations)
    t_pypy = get_average_time(run_pypy, [N], iterations=iterations)
    t_rpyc = get_average_time_compiled(compile_rpython_c, [N], iterations=iterations)
    t_rpyc_jit = get_average_time_compiled(compile_rpython_c_jit, [N], iterations=iterations)
    t_rpyc_mu = get_average_time_compiled(compile_rpython_mu, [N], iterations=iterations)
John Zhang's avatar
John Zhang committed
119
    t_c = get_average_time_compiled(compile_c, [N], iterations=iterations)
120 121 122 123 124 125
    print "CPython:", t_cpython
    print "PyPy (no JIT):", t_pypy_nojit
    print "PyPy:", t_pypy
    print "RPython C:", t_rpyc
    print "RPython C (with JIT):", t_rpyc_jit
    print "RPython Mu Zebu:", t_rpyc_mu
John Zhang's avatar
John Zhang committed
126
    print "C:", t_c
127 128 129

if __name__ == '__main__':
    perf_fibonacci()