Beispiel #1
0
def test_progiter():
    # Define a function that takes some time
    def is_prime(n):
        return n >= 2 and not any(n % i == 0 for i in range(2, n))

    N = 500

    if False:
        file = cStringIO()
        prog = ProgIter(range(N),
                        clearline=False,
                        file=file,
                        freq=N // 10,
                        adjust=False)
        file.seek(0)
        print(file.read())

        prog = ProgIter(range(N), clearline=False)
        for n in prog:
            was_prime = is_prime(n)
            prog.set_extra('n=%r, was_prime=%r' % (
                n,
                was_prime,
            ))
            if (n + 1) % 128 == 0 and was_prime:
                prog.set_extra('n=%r, was_prime=%r EXTRA' % (
                    n,
                    was_prime,
                ))
        file.seek(0)
        print(file.read())

    total = 200
    N = 5000
    N0 = N - total
    print('N = %r' % (N, ))
    print('N0 = %r' % (N0, ))

    print('\n-----')
    print('Demo #0: progress can be disabled and incur essentially 0 overhead')
    print('However, the overhead of enabled progress is minimal and typically '
          'insignificant')
    print('this is verbosity mode verbose=0')
    sequence = (is_prime(n) for n in range(N0, N))
    # with ub.Timer('demo0'):
    if True:
        psequence = ProgIter(sequence,
                             total=total,
                             desc='demo0',
                             enabled=False)
        list(psequence)

    print('\n-----')
    print('Demo #1: progress is shown by default in the same line')
    print('this is verbosity mode verbose=1')
    sequence = (is_prime(n) for n in range(N0, N))
    # with ub.Timer('demo1'):
    if True:
        psequence = ProgIter(sequence, total=total, desc='demo1')
        list(psequence)

    # Default behavior adjusts frequency of progress reporting so
    # the performance of the loop is minimally impacted
    print('\n-----')
    print('Demo #2: clearline=False prints multiple lines.')
    print('Progress is only printed as needed')
    print('Notice the adjustment behavior of the print frequency')
    print('this is verbosity mode verbose=2')
    # with ub.Timer('demo2'):
    if True:
        sequence = (is_prime(n) for n in range(N0, N))
        psequence = ProgIter(sequence,
                             total=total,
                             clearline=False,
                             desc='demo2')
        list(psequence)
        # import utool as ut
        # print(ut.repr4(psequence.__dict__))

    print('\n-----')
    print('Demo #3: Adjustments can be turned off to give constant feedback')
    print('this is verbosity mode verbose=3')
    sequence = (is_prime(n) for n in range(N0, N))
    # with ub.Timer('demo3'):
    if True:
        psequence = ProgIter(sequence,
                             total=total,
                             adjust=False,
                             clearline=False,
                             freq=100,
                             desc='demo3')
        list(psequence)
Beispiel #2
0
def time_progiter_overhead():
    # Time the overhead of this function
    import timeit
    import textwrap
    setup = textwrap.dedent('''
        from sklearn.externals.progiter import ProgIter
        import numpy as np
        import time
        from six.moves import cStringIO, range
        import utool as ut
        N = 500
        file = cStringIO()
        rng = np.random.RandomState(42)
        ndims = 2
        vec1 = rng.rand(113, ndims)
        vec2 = rng.rand(71, ndims)

        def minimal_wraper1(sequence):
            for item in sequence:
                yield item

        def minimal_wraper2(sequence):
            for count, item in enumerate(sequence, start=1):
                yield item

        def minimal_wraper3(sequence):
            count = 0
            for item in sequence:
                yield item
                count += 1

        def minwrap4(sequence):
            for count, item in enumerate(sequence, start=1):
                yield item
                if count % 100:
                    pass

        def minwrap5(sequence):
            for count, item in enumerate(sequence, start=1):
                yield item
                if time.time() < 100:
                    pass
        ''')
    statements = {
        'baseline':
        '[{work} for n in range(N)]',
        'creation':
        'ProgIter(range(N))',
        'minwrap1':
        '[{work} for n in minimal_wraper1(range(N))]',
        'minwrap2':
        '[{work} for n in minimal_wraper2(range(N))]',
        'minwrap3':
        '[{work} for n in minimal_wraper3(range(N))]',
        'minwrap4':
        '[{work} for n in minwrap4(range(N))]',
        'minwrap5':
        '[{work} for n in minwrap5(range(N))]',
        '(sk-disabled)':
        '[{work} for n in ProgIter(range(N), enabled=False, file=file)]',  # NOQA
        '(sk-plain)':
        '[{work} for n in ProgIter(range(N), file=file)]',  # NOQA
        '(sk-freq)':
        '[{work} for n in ProgIter(range(N), file=file, freq=100)]',  # NOQA
        '(sk-no-adjust)':
        '[{work} for n in ProgIter(range(N), file=file, adjust=False, freq=200)]',  # NOQA
        '(sk-high-freq)':
        '[{work} for n in ProgIter(range(N), file=file, adjust=False, freq=200)]',  # NOQA

        # '(ut-disabled)'  : '[{work} for n in ut.ProgIter(range(N), enabled=False, file=file)]',    # NOQA
        # '(ut-plain)'     : '[{work} for n in ut.ProgIter(range(N), file=file)]',  # NOQA
        # '(ut-freq)'      : '[{work} for n in ut.ProgIter(range(N), freq=100, file=file)]',  # NOQA
        # '(ut-no-adjust)' : '[{work} for n in ut.ProgIter(range(N), freq=200, adjust=False, file=file)]',  # NOQA
        # '(ut-high-freq)' : '[{work} for n in ut.ProgIter(range(N), file=file, adjust=False, freq=200)]',  # NOQA
    }
    # statements = {
    #     'calc_baseline': '[vec1.dot(vec2.T) for n in range(M)]',  # NOQA
    #     'calc_plain': '[vec1.dot(vec2.T) for n in ProgIter(range(M), file=file)]',  # NOQA
    #     'calc_plain_ut': '[vec1.dot(vec2.T) for n in ut.ProgIter(range(M), file=file)]',  # NOQA
    # }
    timeings = {}

    work_strs = [
        'None',
        'vec1.dot(vec2.T)',
        'n % 10 == 0',
    ]
    work = work_strs[0]
    # work = work_strs[1]

    number = 10000
    prog = ProgIter(desc='timing', adjust=True)
    for key, stmt in prog(statements.items()):
        prog.set_extra(key)
        secs = timeit.timeit(stmt.format(work=work), setup, number=number)
        timeings[key] = secs / number