diff --git a/per4m/example1.py b/per4m/example1.py index 14134cf..8c88378 100644 --- a/per4m/example1.py +++ b/per4m/example1.py @@ -1,21 +1,35 @@ +# same as example1, but without explicit viztracer calls import threading import time -import viztracer -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 -with viztracer.VizTracer(output_file="example1.json"): - 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/example2.py b/per4m/example2.py index 8c88378..c206650 100644 --- a/per4m/example2.py +++ b/per4m/example2.py @@ -1,6 +1,7 @@ -# same as example1, but without explicit viztracer calls +# shows high gil load, but no wait import threading import time +import numpy as np # if we don't run with gil_load, we just skip it import gil_load @@ -11,25 +12,35 @@ try: except RuntimeError: use_gil_load = False -def some_computation(): + +N = 1024*1024*32 +M = 4 +x = np.arange(N, dtype='f8') + + +def some_numpy_computation(): total = 0 - for i in range(1_000_000): - total += i + for i in range(M): + total += x.sum() return total -thread1 = threading.Thread(target=some_computation) -thread2 = threading.Thread(target=some_computation) +thread1 = threading.Thread(target=some_numpy_computation) +thread2 = threading.Thread(target=some_numpy_computation) + + def main(args=None): thread1.start() thread2.start() - time.sleep(0.2) + total = 0 + for i in range(2_000_000): + total += i 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()