File: threads-test.py

package info (click to toggle)
scalene 1.5.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,528 kB
  • sloc: cpp: 22,930; python: 13,403; javascript: 11,769; ansic: 817; makefile: 196; sh: 45
file content (55 lines) | stat: -rwxr-xr-x 1,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import threading
import sys
import numpy as np

# Disable the @profile decorator if none has been declared.

try:
    # Python 2
    import __builtin__ as builtins
except ImportError:
    # Python 3
    import builtins

try:
    builtins.profile
except AttributeError:
    # No line profiler, provide a pass-through version
    def profile(func): return func
    builtins.profile = profile


class MyThread(threading.Thread):
    @profile
    def run(self):
        z = 0
        z = np.random.uniform(0,100,size=2 * 5000);
        # print("thread1")


class MyThread2(threading.Thread):
    @profile
    def run(self):
        z = 0
        for i in range(5000 // 2):
            z += 1
        # print("thread2")
            

use_threads = True
# use_threads = False

if use_threads:
    for i in range(10000):
        t1 = MyThread()
        t2 = MyThread2()
        t1.start()
        t2.start()
        t1.join()
        t2.join()
else:
    t1 = MyThread()
    t1.run()
    t2 = MyThread2()
    t2.run()