| 1 |
import threading |
| 2 |
import sys |
| 3 |
import numpy as np |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
try: |
| 8 |
|
| 9 |
import __builtin__ as builtins |
| 10 |
except ImportError: |
| 11 |
|
| 12 |
import builtins |
| 13 |
|
| 14 |
try: |
| 15 |
builtins.profile |
| 16 |
except AttributeError: |
| 17 |
|
| 18 |
def profile(func): |
| 19 |
return func |
| 20 |
|
| 21 |
builtins.profile = profile |
| 22 |
|
| 23 |
|
| 24 |
class MyThread(threading.Thread): |
| 25 |
@profile |
| 26 |
def run(self): |
| 27 |
z = 0 |
| 28 |
z = np.random.uniform(0, 100, size=2 * 5000) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class MyThread2(threading.Thread): |
| 33 |
@profile |
| 34 |
def run(self): |
| 35 |
z = 0 |
| 36 |
for i in range(5000 // 2): |
| 37 |
z += 1 |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
use_threads = True |
| 42 |
|
| 43 |
|
| 44 |
if use_threads: |
| 45 |
for i in range(10000): |
| 46 |
t1 = MyThread() |
| 47 |
t2 = MyThread2() |
| 48 |
t1.start() |
| 49 |
t2.start() |
| 50 |
t1.join() |
| 51 |
t2.join() |
| 52 |
else: |
| 53 |
t1 = MyThread() |
| 54 |
t1.run() |
| 55 |
t2 = MyThread2() |
| 56 |
t2.run() |