summaryrefslogtreecommitdiff
path: root/p3time.py
blob: 35e14c9626e7ffb09cf1923c7d01c6e4dc4ab8df (plain)
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
"""Compare timing of plain vs. yield-from calls."""

import gc
import time

def plain(n):
    if n <= 0:
        return 1
    l = plain(n-1)
    r = plain(n-1)
    return l + 1 + r

def coroutine(n):
    if n <= 0:
        return 1
    l = yield from coroutine(n-1)
    r = yield from coroutine(n-1)
    return l + 1 + r

def submain(depth):
    t0 = time.time()
    k = plain(depth)
    t1 = time.time()
    fmt = ' {} {} {:-9,.5f}'
    delta0 = t1-t0
    print(('plain' + fmt).format(depth, k, delta0))

    t0 = time.time()
    try:
        g = coroutine(depth)
        while True:
            next(g)
    except StopIteration as err:
        k = err.value
        t1 = time.time()
        delta1 = t1-t0
        print(('coro.' + fmt).format(depth, k, delta1))
    if delta0:
        print(('relat' + fmt).format(depth, k, delta1/delta0))

def main(reasonable=16):
    gc.disable()
    for depth in range(reasonable):
        submain(depth)

if __name__ == '__main__':
    main()