コード例 #1
0
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))],
    )
    common.run(
        'fib',
コード例 #2
0
ファイル: power.py プロジェクト: Alkalit/cpmoptimize
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)),
        ],
    )
コード例 #3
0
ファイル: fib.py プロジェクト: Alkalit/cpmoptimize
    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))],
    )
    common.run(
        'fib', None,
        common.optimized(naive, iters_limit=10000),
コード例 #4
0
ファイル: arith_sum.py プロジェクト: cmplx/cpmoptimize
    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)),
        ],
        exec_compare=False, draw_plot=False,
    )
コード例 #5
0
ファイル: int_operations.py プロジェクト: Alkalit/cpmoptimize
    # Test that local and global variables were successfully assigned
    # after the ending of the loop
    res += global_var + local_var

    r = 3
    # Loop with unused counter
    for it1 in xrange(-3231, n):
        r = r - 33 + d * 2

    q = 43
    # Loop with used and modified counter
    for it2 in xrange(-3231, n, 4345):
        q = (3 + 4) * q - (it2 * 2 - 8) * 3
        it2 = q + 3 - e

    # Empty loop
    for it3 in xrange(n):
        pass

    # Return ordered list with values of all local variables
    return tuple(sorted(locals().items(), key=lambda item: item[0]))

if __name__ == '__main__':
    common.run(
        'int_operations', None,
        common.optimized(naive),
        [
            (None, 'linear', common.linear_scale(600000, 15)),
        ],
    )
コード例 #6
0
def _optimal(first, count):
    # Returns (sum, coeff ** count)

    if count & 1:
        sub_sum, sub_pow = _optimal(first, count - 1)
        return first + sub_sum * coeff, sub_pow * coeff
    if not count:
        return 0, 1
    sub_sum, sub_pow = _optimal(first, count >> 1)
    return sub_sum * (1 + sub_pow), sub_pow * sub_pow


def optimal(count):
    """Optimal algorithm based on the idea similar to the binary
    exponentiation."""

    return _optimal(start, count)[0]


if __name__ == '__main__':
    common.run(
        'geom_sum',
        None,
        common.optimized(naive) + [
            ('optimal', optimal),
        ],
        [
            (None, 'linear', common.linear_scale(10000, 15)),
        ],
    )
コード例 #7
0
    """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)),
        ],
    )
コード例 #8
0
ファイル: geom_sum.py プロジェクト: Alkalit/cpmoptimize
        elem *= coeff
    return res

def _optimal(first, count):
    # Returns (sum, coeff ** count)

    if count & 1:
        sub_sum, sub_pow = _optimal(first, count - 1)
        return first + sub_sum * coeff, sub_pow * coeff
    if not count:
        return 0, 1
    sub_sum, sub_pow = _optimal(first, count >> 1)
    return sub_sum * (1 + sub_pow), sub_pow * sub_pow

def optimal(count):
    """Optimal algorithm based on the idea similar to the binary
    exponentiation."""

    return _optimal(start, count)[0]

if __name__ == '__main__':
    common.run(
        'geom_sum', None,
        common.optimized(naive) + [
            ('optimal', optimal),
        ],
        [
            (None, 'linear', common.linear_scale(10000, 15)),
        ],
    )
コード例 #9
0
    res += global_var + local_var

    r = 3
    # Loop with unused counter
    for it1 in xrange(-3231, n):
        r = r - 33 + d * 2

    q = 43
    # Loop with used and modified counter
    for it2 in xrange(-3231, n, 4345):
        q = (3 + 4) * q - (it2 * 2 - 8) * 3
        it2 = q + 3 - e

    # Empty loop
    for it3 in xrange(n):
        pass

    # Return ordered list with values of all local variables
    return tuple(sorted(locals().items(), key=lambda item: item[0]))


if __name__ == '__main__':
    common.run(
        'int_operations',
        None,
        common.optimized(naive),
        [
            (None, 'linear', common.linear_scale(600000, 15)),
        ],
    )