diff --git a/README.md b/README.md index 378ffe6..3d9b878 100644 --- a/README.md +++ b/README.md @@ -133,15 +133,15 @@ N = 1024*1024*32 M = 4 x = np.arange(N, dtype='f8') -def run(): +def some_numpy_computation(): total = 0 for i in range(M): total += x.sum() return total -thread1 = threading.Thread(target=run) -thread2 = threading.Thread(target=run) +thread1 = threading.Thread(target=some_numpy_computation) +thread2 = threading.Thread(target=some_numpy_computation) def main(args=None): thread1.start() diff --git a/per4m/example2.py b/per4m/example2.py index 33c3bcd..8c88378 100644 --- a/per4m/example2.py +++ b/per4m/example2.py @@ -1,21 +1,35 @@ # same as example1, but without explicit viztracer calls import threading import time -import time +# if we don't run with gil_load, we just skip it +import gil_load +try: + gil_load.init() + gil_load.start() + use_gil_load = True +except RuntimeError: + use_gil_load = False -def run(): +def some_computation(): total = 0 for i in range(1_000_000): total += i return total -thread1 = threading.Thread(target=run) -thread2 = threading.Thread(target=run) +thread1 = threading.Thread(target=some_computation) +thread2 = threading.Thread(target=some_computation) def main(args=None): thread1.start() thread2.start() time.sleep(0.2) for thread in [thread1, thread2]: thread.join() + if use_gil_load: + gil_load.stop() + stats = gil_load.get() + print(gil_load.format(stats)) + +if __name__ == "__main__": + main() diff --git a/per4m/example3.py b/per4m/example3.py index 0975979..c206650 100644 --- a/per4m/example3.py +++ b/per4m/example3.py @@ -7,6 +7,7 @@ import numpy as np import gil_load try: gil_load.init() + gil_load.start() use_gil_load = True except RuntimeError: use_gil_load = False @@ -16,24 +17,23 @@ N = 1024*1024*32 M = 4 x = np.arange(N, dtype='f8') -def run(): + +def some_numpy_computation(): total = 0 for i in range(M): total += x.sum() return total -if use_gil_load: - gil_load.start() +thread1 = threading.Thread(target=some_numpy_computation) +thread2 = threading.Thread(target=some_numpy_computation) -thread1 = threading.Thread(target=run) -thread2 = threading.Thread(target=run) def main(args=None): thread1.start() thread2.start() total = 0 - for i in range(1_000_000): + for i in range(2_000_000): total += i for thread in [thread1, thread2]: thread.join() @@ -42,3 +42,5 @@ def main(args=None): gil_load.stop() stats = gil_load.get() print(gil_load.format(stats)) +if __name__ == "__main__": + main()