Hacker News new | ask | show | jobs
by takeda 3294 days ago
If your code looks like this: https://github.com/MikeMirzayanov/binary-heap-benchmark/blob...

Then you probably should not use Python, python is more of a glue language which you should strive to make your program looking like a business logic, in real word to solve this problem you would write code such as this:

    import time

    if __name__ == "__main__":
        start = time.clock()
        N = 10000000
        h = list(range(N))
        h.sort()
        for i, v in enumerate(h):
            assert(i == v)
        print("Done in %f" % ((time.clock() - start) * 1000))

    $ python3.6 heap.py
    Done in 2389.877000
Or if heap needs to be used:

    import time
    from heapq import heapify, heappop

    if __name__ == "__main__":
        start = time.clock()
        N = 10000000
        h = list(range(N))
        heapify(h)
        for i in range(len(h)):
            assert(i == heappop(h))
        print("Done in %f" % ((time.clock() - start) * 1000))

    $ python3.6 heap.py
    Done in 10716.348000
Micro benchmarks are silly because you'll never do those things in real code.