Exemplo n.º 1
0
def binary_exp(n):
    """Binary exponentiation algorithm"""

    cur = base
    res = 1
    if not n:
        return res
    while True:
        if n & 1:
            res *= cur
            if n == 1:
                return res
        cur *= cur
        n >>= 1

def built_in(n):
    return base ** n

if __name__ == '__main__':
    common.run(
        'power', None,
        common.optimized(naive) + [
            ('binary', binary_exp),
            ('built-in', built_in),
        ],
        [
            (None, 'linear', common.linear_scale(10000, 15)),
        ],
    )
Exemplo n.º 2
0
def naive(count):
    res = 0
    for elem in xrange(start, start + step * count, step):
        res += elem
    return res

def formula(count):
    return (start * 2 + step * (count - 1)) * count / 2

pow10_wrapper = lambda func: (lambda arg: func(10 ** arg))

if __name__ == '__main__':
    common.run(
        'arith_sum', 'N elements',
        common.optimized(naive) + [
            ('formula', formula),
        ],
        [
            ('linear', None, common.linear_scale(600000, 5)),
        ],
        exec_compare=False, draw_plot=False,
    )
    common.run(
        'arith_sum', '10 ** N elements',
        [
            ('cpm', pow10_wrapper(cpmoptimize()(naive))),
            ('formula', pow10_wrapper(formula)),
        ],
        [
            ('exp', None, common.linear_scale(10000, 5)),
Exemplo n.º 3
0
    return c, d


def fast_doubling(n):
    """Fast doubling - implementation via matrix exponentiation
    with the redundant calculations removed."""

    return _fast_doubling(n)[0]


if __name__ == '__main__':
    # Test all functions on different testcases
    common.run(
        'fib',
        None,
        common.optimized(naive) + [
            ('matrices', classic_matrices),
            ('fast dbl', fast_doubling),
        ],
        [
            ('small', 'linear', common.linear_scale(30000, 15)),
            ('big', 'linear', common.linear_scale(300000, 15)),
        ],
    )

    # Test optimized function with different settings
    common.run(
        'fib',
        None,
        common.optimized(naive),
        [('demo', 'linear', common.linear_scale(60000, 30))],
Exemplo n.º 4
0
    d = b * b + a * a
    if n & 1:
        return d, c + d
    return c, d

def fast_doubling(n):
    """Fast doubling - implementation via matrix exponentiation
    with the redundant calculations removed."""

    return _fast_doubling(n)[0]

if __name__ == '__main__':
    # Test all functions on different testcases
    common.run(
        'fib', None,
        common.optimized(naive) + [
            ('matrices', classic_matrices),
            ('fast dbl', fast_doubling),
        ],
        [
            ('small', 'linear', common.linear_scale(30000, 15)),
            ('big', 'linear', common.linear_scale(300000, 15)),
        ],
    )

    # Test optimized function with different settings
    common.run(
        'fib', None,
        common.optimized(naive),
        [('demo', 'linear', common.linear_scale(60000, 30))],
    )