示例#1
0
    number_of_times = 100000
    try:
        number_of_times = int(sys.argv[2])
    except Exception:
        pass

    # The main top-level CFFI class that you instantiate once
    ffi = cffi.FFI()

    # Parses the given C source.  This registers all declared functions.
    ffi.cdef('int compute_fibonacci(int n);')

    # Load and return a dynamic library.  The standard C library can be loaded by passing None.
    libfib = ffi.dlopen('./libfibonacci.so')

    fib_py = fib_python.compute_fibonacci(n)
    fib_cffi = libfib.compute_fibonacci(n)
    if fib_py != fib_cffi:
        raise (ValueError(fib_cffi))

    py_tot = timeit.timeit("compute_fibonacci({})".format(n),
                           setup="from fib_python import compute_fibonacci",
                           number=number_of_times)
    cffi_tot = timeit.timeit(
        "libfib.compute_fibonacci({})".format(n),
        setup=
        """import cffi; ffi = cffi.FFI(); ffi.cdef('int compute_fibonacci(int n);'); libfib = ffi.dlopen('./libfibonacci.so')""",
        number=number_of_times)
    py_avg = py_tot / number_of_times
    cffi_avg = cffi_tot / number_of_times
示例#2
0
    import sys
    import timeit

    n = 20
    try:
        n = int(sys.argv[1])
    except Exception:
        pass

    number_of_times = 100000
    try:
        number_of_times = int(sys.argv[2])
    except Exception:
        pass

    fib_py = compute_fibonacci(n)
    fib_cy = compute_fibonacci_wrapper(n)
    if fib_py != fib_cy:
        raise (ValueError(fib_cy))

    py_tot = timeit.timeit("compute_fibonacci({})".format(n),
                           setup="from fib_python import compute_fibonacci",
                           number=number_of_times)
    cy_tot = timeit.timeit("compute_fibonacci_wrapper({})".format(n),
                           setup="from cyfib import compute_fibonacci_wrapper",
                           number=number_of_times)
    py_avg = py_tot / number_of_times
    cy_avg = cy_tot / number_of_times

    print("fib({}) = {}".format(n, fib_py))
    print("Python average time:  {0:.2g}".format(py_avg))