ffi-overhead icon indicating copy to clipboard operation
ffi-overhead copied to clipboard

suggest adding a python version

Open windbender opened this issue 6 years ago • 2 comments

Python is another language which commonly calls into C functions.

windbender avatar May 27 '18 16:05 windbender

Anyone want to have a crack at it? (PR)

dyu avatar May 30 '18 01:05 dyu

@dyu Don't actually want to make PR, because I don't want to install all the dependencies to make clean test before submitting it :blush: But code would be something like this:

import sys
from ctypes import cdll

lib = cdll.LoadLibrary("newplus/libplus.so")


def run(count):
    start = lib.current_timestamp()

    x = 0
    while x < count:
        x = lib.plusone(x)

    print(lib.current_timestamp() - start)


def main():
    if len(sys.argv) == 1:
        print("First arg (0 - 2000000000) is required.")
        return

    try:
        count = int(sys.argv[1])
        if count <= 0 or count > 2000000000:
            raise ValueError
    except ValueError:
        print("Must be a positive number not exceeding 2 billion.")
        return

    run(count)


if __name__ == '__main__':
    main()

fly avatar Nov 16 '18 15:11 fly