scoder / fastrlock

A fast RLock implementation for CPython
MIT License
23 stars 10 forks source link

Not fast? #2

Closed sonots closed 7 years ago

sonots commented 7 years ago

I wrote benchmark scripts as follows to compare:

lock_bench.py

import time
import threading

lock = threading.RLock()

start_time = time.time()
for i in range(100000):
    lock.acquire()
    lock.release()
    # with lock:
    #     pass
elapsed_time = time.time() - start_time
print(elapsed_time)

fastrlock_bench.py

import time
from fastrlock import rlock

lock = rlock.FastRLock()

start_time = time.time()
for i in range(100000):
    lock.acquire()
    lock.release()
    # with lock:
    #     pass
elapsed_time = time.time() - start_time
print(elapsed_time)

I obtained following results which show fastrlock is not fast.

$ python ~/lock_bench.py
0.03992772102355957
$ python ~/fastrlock_bench.py
0.04404258728027344

My python and cython version is as follows:

$ python --version
Python 3.6.1
$ cython --version
Cython version 0.25.2

Could you tell me if I am doing something wrong?

scoder commented 7 years ago

Interesting. I hadn't run benchmarks in a while. Looks like threading.RLock is faster starting with Py3.4. FastRLock is essentially unchanged since 2010. Probably no longer worth using these days then (well, except in Python 2 where the advantage is huge, or maybe also when called directly from Cython code).

sonots commented 7 years ago

Thank you!

sonots commented 7 years ago

Additional comments:

In the case of cython with FastRLock C API, fastrlock was pretty faster than threading.RLock even in Py3.6. Thanks!

scoder commented 7 years ago

Thanks for reporting back!