示例#1
0
def time_query_using_module(module):
    # This is largely copied verbatim from the 'timeit' module
    repeat = 3
    number = 10
    verbose = True
    precision = 3
    stmt = partial(query_using_greenlets, module)
    t = Timer(stmt)
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    best = min(r)
    if verbose:
        print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
    print("%s: %d loops," % (module.__name__, number))
    usec = best * 1e6 / number
    if usec < 1000:
        print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
    else:
        msec = usec / 1000
        if msec < 1000:
            print("best of %d: %.*g msec per loop" % (repeat, precision, msec))
        else:
            sec = msec / 1000
            print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
示例#2
0
def main():
    t = Timer( 'euler14()', "from __main__ import euler14" )

    try:
        print t.timeit( 1 )
    except:
        print t.print_exc()
示例#3
0
def time_query_using_module(module):
    # This is largely copied verbatim from the 'timeit' module
    repeat = 3
    number = 10
    verbose = True
    precision = 3
    stmt = partial(query_using_greenlets, module)
    t = Timer(stmt)
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    best = min(r)
    if verbose:
        print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
    print("%s: %d loops," % (module.__name__, number))
    usec = best * 1e6 / number
    if usec < 1000:
        print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
    else:
        msec = usec / 1000
        if msec < 1000:
            print("best of %d: %.*g msec per loop" % (repeat, precision, msec))
        else:
            sec = msec / 1000
            print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
示例#4
0
文件: bench.py 项目: steveb/pint
def time_stmt(stmt='pass', setup='pass', number=0, repeat=3):
    """Timer function with the same behaviour as running `python -m timeit `
    in the command line.

    :return: elapsed time in seconds or NaN if the command failed.
    :rtype: float
    """

    t = Timer(stmt, setup)

    if not number:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i

            try:
                x = t.timeit(number)
            except:
                print(t.print_exc())
                return float('NaN')

            if x >= 0.2:
                break

    try:
        r = t.repeat(repeat, number)
    except:
        print(t.print_exc())
        return float('NaN')

    best = min(r)

    return best / number
示例#5
0
def time_stmt(stmt='pass', setup='pass', number=0, repeat=3):
    """Timer function with the same behaviour as running `python -m timeit `
    in the command line.

    :return: elapsed time in seconds or NaN if the command failed.
    :rtype: float
    """

    t = Timer(stmt, setup)

    if not number:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i

            try:
                x = t.timeit(number)
            except:
                print(t.print_exc())
                return float('NaN')

            if x >= 0.2:
                break

    try:
        r = t.repeat(repeat, number)
    except:
        print(t.print_exc())
        return float('NaN')

    best = min(r)

    return best / number
示例#6
0
    def __execute(self, sql, kwargs):

        db = kwargs['db']
        hostType = kwargs['host_type']
        cursor = None
        if self.clientCursor:
            cursor = self.clientCursor
        else:
            cursor = SQLHub.connection[hostType]['cursor']

        ##Get the proc name for debug message##
        proc = ""
        if 'proc' in kwargs:
            proc = kwargs['proc']

        ##Caller requests no sql execution##
        if 'debug_noex' in kwargs:
            self.showDebug(db,
                           self.conf[hostType]['host'],
                           hostType,
                           proc,
                           sql,
                           None)
            return []

        ##Caller wants to sql execution time##
        if 'debug_show' in kwargs:

            def timewrapper():
                self.__cursorExecute(sql, kwargs, cursor)

            t = Timer(timewrapper)
            tmsg = ""
            try:
                tmsg = t.timeit(1)
            except:
                t.print_exc()

            self.showDebug(db,
                           self.conf[hostType]['host'],
                           hostType,
                           proc,
                           sql,
                           tmsg)
        else:
            self.__cursorExecute(sql, kwargs, cursor)

        ##Commit transaction##
        SQLHub.connection[hostType]['con_obj'].commit()

        return self.getData(cursor, kwargs)
示例#7
0
def main():
    triplet = findPythagoreanTriplet( 1000 )

    if triplet:
        print triplet
        print reduce( lambda x, y: x * y, triplet )
    else:
        print 'Not found :('

    t = Timer( 'findPythagoreanTriplet( 1000 )', "from __main__ import findPythagoreanTriplet" )

    try:
        print t.timeit( 1 )
    except:
        print t.print_exc()
示例#8
0
文件: bench.py 项目: znicholls/pint
def time_stmt(stmt="pass", setup="pass", number=0, repeat=3):
    """Timer function with the same behaviour as running `python -m timeit `
    in the command line.

    Parameters
    ----------
    stmt : str
         (Default value = "pass")
    setup : str
         (Default value = "pass")
    number : int
         (Default value = 0)
    repeat : int
         (Default value = 3)

    Returns
    -------
    float
        elapsed time in seconds or NaN if the command failed.

    """

    t = Timer(stmt, setup)

    if not number:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i

            try:
                x = t.timeit(number)
            except Exception:
                print(t.print_exc())
                return float("NaN")

            if x >= 0.2:
                break

    try:
        r = t.repeat(repeat, number)
    except Exception:
        print(t.print_exc())
        return float("NaN")

    best = min(r)

    return best / number
示例#9
0
def timeit(stmt='pass', setup='pass', number=0, repeat=3):
    """Timer function with the same behaviour as running `python -m timeit `
    in the command line.

    :return: best elapsed time in seconds or NaN if the command failed.
    :rtype: float
    """

    if not setup:
        setup = 'pass'

    if not stmt:
        stmt = 'pass'

    key = (stmt, setup, number, repeat)

    if key in _CACHE:
        return _CACHE[key]

    t = Timer(stmt, setup)

    if not number:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i

            try:
                x = t.timeit(number)
            except:
                print(t.print_exc())
                return float('NaN')

            if x >= 0.2:
                break

    try:
        r = t.repeat(repeat, number)
    except:
        print(t.print_exc())
        return float('NaN')

    result = min(r) / number

    _CACHE[key] = result

    return result
def test_svd():
    k_list = [500, 1000, 1200, 1500, 2000, 3000, 4000, 5000]

    print('Single Value Decomposition (double precision)')
    print('{0:4} {1:10}'.format('k', 'time (ms)'))

    for k in k_list:
        setup = 'import numpy as np; from numpy.random import rand as rn; \
        from numpy.linalg import svd as svd; \
        A = rn(%d, %d)' % (k, k)

        t = Timer('U, s, V = svd(A)', setup)
        try:
            num = 5
            print('{0:4d} {1:6.2f}'.format(k,
                                           min(t.repeat(5, num)) / num * 1000))
        except:
            t.print_exc()
def test_lu():
    k_list = [500, 1000, 1200, 1500, 2000, 3000, 4000, 5000]

    print('LU decomposition (double precision)')
    print('{0:4} {1:10}'.format('k', 'time (ms)'))

    for k in k_list:
        setup = 'import numpy as np; from numpy.random import rand as rn; \
        from scipy.linalg import lu as lu; \
        A = rn(%d, %d)' % (k, k)

        t = Timer('P, L, U = lu(A)', setup)
        try:
            num = 10
            print('{0:4d} {1:6.2f}'.format(k,
                                           min(t.repeat(5, num)) / num * 1000))
        except:
            t.print_exc()
def test_chole():
    k_list = [500, 1000, 1200, 1500, 2000, 3000, 4000, 5000]

    print('Cholesky decomposition (double precision)')
    print('{0:4} {1:10}'.format('k', 'time (ms)'))

    for k in k_list:
        # Generate a random symmetric positive definite matrix.
        setup = 'import numpy as np; from numpy.random import random as rn; \
        from numpy.linalg import cholesky as chole; \
        tl = np.tri(%d, %d, -1, dtype=np.double); \
        tl[tl > 0] = rn((%d - 1) * %d / 2); \
        A = tl + tl.T + (%d - 1) * np.eye(%d, %d)' % (k, k, k, k, k, k, k)

        t = Timer('L = chole(A)', setup)
        try:
            num = 10
            print('{0:4d} {1:6.2f}'.format(k,
                                           min(t.repeat(5, num)) / num * 1000))
        except:
            t.print_exc()
示例#13
0
 def run_bench(bench_env, engine_name=None):
     timer = Timer(setup=setup_code,
                   stmt=bench_code,
                   globals=bench_env)
     try:
         result = min(timer.repeat(repeat=repeat,
                                   number=iterations))
     except Exception:
         message = "Unable to run {} between {} and {}".format(
             target, original_name, revised_name)
         if engine_name:
             message += " with {}".format(engine_name)
         print(message, file=stderr)
         timer.print_exc()
         exit(1)
     result *= 1000
     message = "{}  {:.3f} ms -- {} and {}".format(
         padded_target, result, original_name, revised_name)
     if engine_name:
         message += " with {}".format(engine_name)
     print(message)
示例#14
0
def test_mm(m, n):
    k_list = [
        64, 80, 96, 104, 112, 120, 128, 144, 160, 176, 192, 200, 208, 224
    ]

    print 'Matrix Multiplication (double precision)'
    print '{0:4} {1:4} {2:4} {3:10}'.format('m', 'n', 'k', 'time (ms)')

    for k in k_list:

        setup = 'import numpy; \
        A = numpy.array(numpy.random.rand(%d, %d), dtype = numpy.double); \
        B = numpy.array(numpy.random.rand(%d, %d), dtype = numpy.double)' \
        % (m, n, n, k)

        t = Timer('C = numpy.dot(A, B)', setup)
        try:
            num = 10
            print '{0:4d} {1:4d} {2:4d} {3:6.2f}'.\
                    format(m, n, k, min(t.repeat(5, num))/num*1000)
        except:
            t.print_exc()
示例#15
0
文件: speed.py 项目: RoboTeddy/mom
def report(stmt, setup, number=0, verbose=0, precision=3,
           repeat=default_repeat, timer=default_timer):
  sys.stdout.write("%50s -- " % stmt)
  t = Timer(stmt, setup, timer)
  if number == 0:
    # determine number so that 0.2 <= total time < 2.0
    for i in range(1, 10):
      number = 10 ** i
      try:
        x = t.timeit(number)
      except Exception:
        t.print_exc()
        return 1
      if verbose:
        print("%d loops -> %.*g secs" % (number, precision, x))
      if x >= 0.2:
        break
  try:
    r = t.repeat(repeat, number)
  except Exception:
    t.print_exc()
    return 1
  best = min(r)
  if verbose:
    print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
  sys.stdout.write("%d loops, " % number)
  usec = best * 1e6 / number
  if usec < 1000:
    print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
  else:
    msec = usec / 1000
    if msec < 1000:
      print("best of %d: %.*g msec per loop" % (repeat, precision, msec))
    else:
      sec = msec / 1000
      print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
示例#16
0
def benchmark(number=0, precision=3, verbose=True):
    repeat = default_repeat
    t = Timer(_benchmark)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i
            try:
                x = t.timeit(number)
            except:
                t.print_exc()
                return 1
            if verbose:
                print "%d loops -> %.*g secs" % (number, precision, x)
            if x >= 0.2:
                break
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    best = min(r)
    if verbose:
        print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
    print "%d loops," % number,
    usec = best * 1e6 / number
    if usec < 1000:
        print "best of %d: %.*g usec per loop" % (repeat, precision, usec)
    else:
        msec = usec / 1000
        if msec < 1000:
            print "best of %d: %.*g msec per loop" % (repeat, precision, msec)
        else:
            sec = msec / 1000
            print "best of %d: %.*g sec per loop" % (repeat, precision, sec)
    return None
示例#17
0
def benchmark(number=0, precision=3, verbose=True):
    repeat = default_repeat
    t = Timer(_benchmark)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10 ** i
            try:
                x = t.timeit(number)
            except:
                t.print_exc()
                return 1
            if verbose:
                print "%d loops -> %.*g secs" % (number, precision, x)
            if x >= 0.2:
                break
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    best = min(r)
    if verbose:
        print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
    print "%d loops," % number,
    usec = best * 1e6 / number
    if usec < 1000:
        print "best of %d: %.*g usec per loop" % (repeat, precision, usec)
    else:
        msec = usec / 1000
        if msec < 1000:
            print "best of %d: %.*g msec per loop" % (repeat, precision, msec)
        else:
            sec = msec / 1000
            print "best of %d: %.*g sec per loop" % (repeat, precision, sec)
    return None
示例#18
0
def main(args=None, *, _wrap_timer=None):
    """ Main program, used when run as a script.

        The optional 'args' argument specifies the command line to be parsed,
        defaulting to sys.argv[1:].

        The return value is an exit code to be passed to sys.exit(); it
        may be None to indicate success.

        When an exception happens during timing, a traceback is printed to
        stderr and the return value is 1.  Exceptions at other times
        (including the template compilation) are not caught.

        '_wrap_timer' is an internal interface used for unit testing.  If it
        is not None, it must be a callable that accepts a timer function
        and returns another timer function (used for unit testing).
        """
    if args is None:
        args = sys.argv[1:]
    import getopt
    try:
        opts, args = getopt.getopt(args, "n:u:s:r:tcpvh", [
            "number=", "setup=", "repeat=", "time", "clock", "process",
            "verbose", "unit=", "help"
        ])
    except getopt.error as err:
        print(err)
        print("use -h/--help for command line help")
        return 2

    timer = default_timer
    stmt = "\n".join(args) or "pass"
    number = 0  # auto-determine
    setup = []
    repeat = default_repeat
    verbose = 0
    time_unit = None
    units = {"nsec": 1e-9, "usec": 1e-6, "msec": 1e-3, "sec": 1.0}
    precision = 3
    for o, a in opts:
        if o in ("-n", "--number"):
            number = int(a)
        if o in ("-s", "--setup"):
            setup.append(a)
        if o in ("-u", "--unit"):
            if a in units:
                time_unit = a
            else:
                print(
                    "Unrecognized unit. Please select nsec, usec, msec, or sec.",
                    file=sys.stderr)
                return 2
        if o in ("-r", "--repeat"):
            repeat = int(a)
            if repeat <= 0:
                repeat = 1
        if o in ("-p", "--process"):
            timer = time.process_time
        if o in ("-v", "--verbose"):
            if verbose:
                precision += 1
            verbose += 1
        if o in ("-h", "--help"):
            print(__doc__, end=' ')
            return 0
    setup = "\n".join(setup) or "pass"

    # Include the current directory, so that local imports work (sys.path
    # contains the directory of this script, rather than the current
    # directory)
    import os
    sys.path.insert(0, os.curdir)
    if _wrap_timer is not None:
        timer = _wrap_timer(timer)

    t = Timer(stmt, setup, timer)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        callback = None
        if verbose:

            def callback(number, time_taken):
                msg = "{num} loop{s} -> {secs:.{prec}g} secs"
                plural = (number != 1)
                print(
                    msg.format(num=number,
                               s='s' if plural else '',
                               secs=time_taken,
                               prec=precision))

        try:
            number, _ = t.autorange(callback)
        except:
            t.print_exc()
            return 1

        if verbose:
            print()

    try:
        raw_timings = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1

    def format_time(dt):
        unit = time_unit
        scale: float = 1.0

        if unit is not None:
            scale = units[unit]
        else:
            scales = [(scale, unit) for unit, scale in units.items()]
            scales.sort(reverse=True)
            for scale, unit in scales:
                if dt >= scale:
                    break

        return "%.*g %s" % (precision, dt / scale, unit)

    if verbose:
        print("raw times: %s" % ", ".join(map(format_time, raw_timings)))
        print()
    timings = [dt / number for dt in raw_timings]

    best = min(timings)
    print("%d loop%s, best of %d: %s per loop" %
          (number, 's' if number != 1 else '', repeat, format_time(best)))

    best = min(timings)
    worst = max(timings)
    if worst >= best * 4:
        import warnings
        warnings.warn_explicit(
            "The test results are likely unreliable. "
            "The worst time (%s) was more than four times "
            "slower than the best time (%s)." %
            (format_time(worst), format_time(best)), UserWarning, '', 0)
    return None
示例#19
0
 print 'running repeat: %d number: %d' % (repeat, number)
 setup = "\n".join(setup) or "pass"
 # Include the current directory, so that local imports work (sys.path
 # contains the directory of this script, rather than the current
 # directory)
 import os
 sys.path.insert(0, os.curdir)
 t = Timer(stmt, setup, timer)
 if number == 0:
     # determine number so that 0.2 <= total time < 2.0
     for i in range(1, 10):
         number = 10**i
         try:
             x = t.timeit(number)
         except:
             t.print_exc()
             return 1
         if verbose:
             print "%d loops -> %.*g secs" % (number, precision, x)
         if x >= 0.2:
             break
 t.repeat(1, 100)  # warm up the jit for pypy
 try:
     r = t.repeat(repeat, number)
 except:
     t.print_exc()
     return 1
 if verbose:
     print "raw times:", " ".join(["%.4f" % x for x in sorted(r)])
 #r = [int(x * 1e6 / number) for x in r]
 best = min(r)
示例#20
0
         return 0
 setup = "\n".join(setup) or "pass"
 # Include the current directory, so that local imports work (sys.path
 # contains the directory of this script, rather than the current
 # directory)
 import os
 sys.path.insert(0, os.curdir)
 t = Timer(stmt, setup, timer)
 if number == 0:
     # determine number so that 0.2 <= total time < 2.0
     for i in range(1, 10):
         number = 10**i
         try:
             x = t.timeit(number)
         except:
             t.print_exc()
             return 1
         if verbose:
             print "%d loops -> %.*g secs" % (number, precision, x)
         if x >= 0.2:
             break
 try:
     r = t.repeat(repeat, number)
 except:
     t.print_exc()
     return 1
 if verbose:
     print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
 r = [int(x * 1e6 / number) for x in r]
 best = min(r)
 average = int(numpy.average(r))
def main(args=None):
    """Main program, used when run as a script.

    The optional argument specifies the command line to be parsed,
    defaulting to sys.argv[1:].

    The return value is an exit code to be passed to sys.exit(); it
    may be None to indicate success.

    When an exception happens during timing, a traceback is printed to
    stderr and the return value is 1.  Exceptions at other times
    (including the template compilation) are not caught.
    """
    if not args:
        args = sys.argv[1:]
    import getopt
    try:
        opts, args = getopt.getopt(args, 'n:s:r:tcvh',
                                   ['number=', 'setup=', 'repeat=',
                                    'time', 'clock', 'verbose', 'help'])
    except getopt.error as err:
        print(err)
        print("use -h/--help for command line help")
        return 2
    timer = default_timer
    stmt = "\n".join(args) or "pass"
    number = 0  # auto-determine
    setup = []
    repeat = default_repeat
    verbose = 0
    precision = 3
    for o, a in opts:
        if o in ("-n", "--number"):
            number = int(a)
        if o in ("-s", "--setup"):
            setup.append(a)
        if o in ("-r", "--repeat"):
            repeat = int(a)
            if repeat <= 0:
                repeat = 1
        if o in ("-t", "--time"):
            timer = time.time
        if o in ("-c", "--clock"):
            timer = time.clock
        if o in ("-v", "--verbose"):
            if verbose:
                precision += 1
            verbose += 1
        if o in ("-h", "--help"):
            print(__doc__)
            return 0
    setup = "\n".join(setup) or "pass"
    # Include the current directory, so that local imports work (sys.path
    # contains the directory of this script, rather than the current
    # directory)
    import os
    sys.path.insert(0, os.curdir)
    t = Timer(stmt, setup, timer)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i
            try:
                x = t.timeit(number)
            except:
                t.print_exc()
                return 1
            if verbose:
                print("%d loops -> %.*g secs" % (number, precision, x))
            if x >= 0.2:
                break
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    if verbose:
        print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
    r = [int(x * 1e6 / number) for x in r]
    best = min(r)
    average = int(numpy.average(r))
    std = int(numpy.std(r))

    print(best, average, std)
def main(args=None):
    """Main program, used when run as a script.

    The optional argument specifies the command line to be parsed,
    defaulting to sys.argv[1:].

    The return value is an exit code to be passed to sys.exit(); it
    may be None to indicate success.

    When an exception happens during timing, a traceback is printed to
    stderr and the return value is 1.  Exceptions at other times
    (including the template compilation) are not caught.
    """
    if not args:
        args = sys.argv[1:]
    import getopt
    try:
        opts, args = getopt.getopt(args, 'n:s:r:tcvh', [
            'number=', 'setup=', 'repeat=', 'time', 'clock', 'verbose', 'help'
        ])
    except getopt.error as err:
        print(err)
        print("use -h/--help for command line help")
        return 2
    timer = default_timer
    stmt = "\n".join(args) or "pass"
    number = 0  # auto-determine
    setup = []
    repeat = default_repeat
    verbose = 0
    precision = 3
    for o, a in opts:
        if o in ("-n", "--number"):
            number = int(a)
        if o in ("-s", "--setup"):
            setup.append(a)
        if o in ("-r", "--repeat"):
            repeat = int(a)
            if repeat <= 0:
                repeat = 1
        if o in ("-t", "--time"):
            timer = time.time
        if o in ("-c", "--clock"):
            timer = time.clock
        if o in ("-v", "--verbose"):
            if verbose:
                precision += 1
            verbose += 1
        if o in ("-h", "--help"):
            print(__doc__)
            return 0
    setup = "\n".join(setup) or "pass"
    # Include the current directory, so that local imports work (sys.path
    # contains the directory of this script, rather than the current
    # directory)
    import os
    sys.path.insert(0, os.curdir)
    t = Timer(stmt, setup, timer)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i
            try:
                x = t.timeit(number)
            except:
                t.print_exc()
                return 1
            if verbose:
                print("%d loops -> %.*g secs" % (number, precision, x))
            if x >= 0.2:
                break
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
    if verbose:
        print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
    r = [int(x * 1e6 / number) for x in r]
    best = min(r)
    average = int(numpy.average(r))
    std = int(numpy.std(r))

    print(best, average, std)
示例#23
0
    print("num unique words : {nw} ".format(nw=len(words_unique)))
    print("head : ", *words_unique[1:25], sep=' - ')
    print("tail : ", *words_unique[len(words_unique)-25:], sep=' - ')
    return words_unique


def get_unique_words_in_file2(file_path, min_length):
    text = read_as_text(file_path)
    words = get_words_as_list(text, min_length)
    words_unique = list(set(words))
    locale.setlocale(locale.LC_ALL, '')
    words_unique.sort(cmp=locale.strcoll)
    print("num unique words : {nw} ".format(nw=len(words_unique)))
    print("head : ", *words_unique[1:25], sep=' - ')
    print("tail : ", *words_unique[len(words_unique)-25:], sep=' - ')
    return words_unique


if __name__ == "__main__":
    from timeit import Timer
    myFilename = "sample_data/Victor_Hugo-Les_miserables_Tome_1_UTF8.txt"
    t1 = Timer("get_unique_words_in_file1(myFilename, 3)",
               setup="from __main__ import get_unique_words_in_file1,myFilename")
    t1.timeit(1)
    t1.print_exc()
    t2= Timer("get_unique_words_in_file2(myFilename, 3)",
              setup="from __main__ import get_unique_words_in_file2,myFilename")
    t2.timeit(1)
    t2.print_exc()

示例#24
0

# ref:  https://docs.python.org/3/library/timeit.html
# Seems: when use __slots__ or __dict__ in withSlots class, U can see the speedup
# but when U use __dict__ in withoutSlots, it has bigger latency.
# the results need to be re-evaluated !!!

if __name__ == '__main__':
    print("test slots with/without")
    number = 50000
    x = 1
    y = 10
    z = 100
    var_withslots = withSlots(x, y, z)
    var_withoutslots = withoutSlots(x, y, z)
    T = Timer()

    try:
        #with_time = timeit.timeit(instance_fn(withSlots), number=number)
        with_time = T.timeit(get_set_fn(withSlots), number=number)
        print('withSlots time = {}'.format(with_time))
    except:
        T.print_exc()

    try:
        #without_time = timeit.timeit(instance_fn(withoutSlots), number=number)
        without_time = T.timeit(get_set_fn(withoutSlots), number=number)
        print('withoutSlots time = {}'.format(without_time))
    except:
        T.print_exc()