benchmark.pyΒΆ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Benchmark PScript, to compare with CPython, Pypy, Brython, etc.

It appears that PScript is significantly faster than Brython, which
seems to be the fastest Python-in-the-brower so far. It is also faster
than CPython and for some tests on par with Pypy.

See also http://brythonista.wordpress.com/2015/03/28

PyStone seems unavailable in Python 3.6 (?), so its commented out.

"""

# Measured results, in pystones/second, measured on 05-03-2016,
# on Win 10, Intel i7-4710 HQ 2.5GHz
RESULTS = [(124863, 'CPython 3.4', 'blue'),
           (137250, 'CPython 3.5', 'blue'),
           (3927770, 'Pypy4', 'blue'),
           (3739170, 'Pypy3', 'blue'),
           (127957, 'PScript on Firefox', 'orange'),
           (79517, 'PScript on Chrome', 'orange'),
           (128325, 'PScript on MS Edge', 'orange'),
           (2982, 'Brython', 'magenta'),
           (2780, 'Skulpt', 'magenta'),
           (268817, 'PypyJS', 'magenta'),
           ]

import sys
from time import time
import platform
# from test.pystone import main as pystone_main
# from test import pystone

from flexx import app, event

# Mark the pystone module to be transpiled as a whole. It uses globals
# a lot, which somehow causes inifinite loops if its transpiled in parts.
# pystone.__pscript__ = True

# Backend selection
BACKEND = 'firefox-app or chrome-app'
if sys.argv[1:]:
    BACKEND = sys.argv[1]


def plot_results():
    import matplotlib.pyplot as plt
    plt.ion()

    data = list(reversed(RESULTS))
    plt.figure(1)
    plt.clf()
    ax = plt.subplot(111)
    ax.barh([i for i in range(len(data))], [x[0] for x in data],
            color=[x[2] for x in data])
    ax.set_yticks([i+0.3 for i in range(len(data))])
    ax.set_yticklabels([x[1] for x in data])
    ax.set_xscale('log')


class window:
    # Trick to be able to use the same code in JS and Python

    @classmethod
    def Float32Array(cls, n):
        """ Factory function. """
        return [0.0] * n


def convolve():
    N = 400000
    data = window.Float32Array(N)
    support = 3
    t0 = time()
    for i in range(support, N-support):
        for j in range(-support, support+1):
            data[i] += data[i+j] * (1/support*2)
    t1 = time()
    print('convolution took %f s' % (t1-t0))


def bench_str():
    """ From http://brythonista.wordpress.com/2015/03/28
    """
    print('String benchmarks:')

    t0 = time()
    for i in range(1000000):
        a = 1
    print("  assignment.py", time()-t0)

    t0 = time()
    a = 0
    for i in range(1000000):
        a += 1
    print("  augm_assign.py", time()-t0)

    t0 = time()
    for i in range(1000000):
        a = 1.0
    print("  assignment_float.py", time()-t0)

    t0 = time()
    for i in range(1000000):
        a = {0: 0}
    print("  build_dict.py", time()-t0)

    t0 = time()
    a = {0: 0}

    for i in range(1000000):
        a[0] = i

    assert a[0]==999999
    print("  set_dict_item.py", time()-t0)

    t0 = time()
    for i in range(1000000):
        a = [1, 2, 3]
    print("  build_list.py", time()-t0)

    t0 = time()
    a = [0]

    for i in range(1000000):
        a[0] = i
    print("  set_list_item.py", time()-t0)

    t0 = time()
    a, b, c = 1, 2, 3
    for i in range(1000000):
        a + b + c
    print("  add_integers.py", time()-t0)

    t0 = time()
    a, b, c = 'a', 'b', 'c'
    for i in range(1000000):
        a + b + c
    print("  add_strings.py", time()-t0)

    t0 = time()
    for _i in range(100000):
        str(_i)
    print("  str_of_int.py", time()-t0)

    t0 = time()
    for i in range(1000000):
        def f():
            pass
    print("  create_function.py", time()-t0)

    t0 = time()
    def g(x):
        return x
    for i in range(1000000):
        g(i)
    print("  function_call.py", time()-t0)


class BenchmarkerPy(app.PyComponent):

    @event.action
    def benchmark(self):
        print('\n==== Python %s %s =====\n' % (platform.python_implementation(),
                                               platform.python_version()))
        # pystone_main()
        convolve()
        bench_str()


class BenchmarkerJs(app.JsComponent):

    @event.action
    def benchmark(self):
        print()
        print('==== PScript on %s =====' % BACKEND)
        print()
        # pystone_main()
        convolve()
        bench_str()


b1 = app.launch(BenchmarkerPy, BACKEND)
with b1:
    b2 = BenchmarkerJs()
b1.benchmark()
b2.benchmark()

app.run()