为什么每个线程运行时间不同
I create an array with 1000000 element and each element is random, I have two question
- Why each thread run time is different and every time I run code time is different ?线程的运行时间取决于什么?
- 为什么顺序比多线程更快?
Thank you very much
def Result(arr,n):
sum = 0
for i in arr: sum += i
arr_list[n] = sum
def Thread_func(number):
arr_thread = []
start = perf_counter()
for i in range(number):
head = int(i*1000000/number)
tail = int((i+1)*1000000/number)
thread = threading.Thread(target=Result, args=(randnums[head:tail],i))
arr_thread.append(thread)
thread.start()
for thread in arr_thread:
thread.join()
end = perf_counter()
print('Multithread with {} threads fishined in {} s'.format(number, end - start))
random_arr = [random.choice(range(1000000)) for _ in range(1000000)]
arr = [2,3,4,5,6,7,8,9,10]
arr_list = list(range(max(arr)))
for i in arr:
Thread_func(i)
I create an array with 1000000 element and each element is random, I have two question
- Why each thread run time is different and every time I run code time is different ? What does the running time of thread depend on ?
- Why Sequential is faster than Multithread ?
Thank you very much
def Result(arr,n):
sum = 0
for i in arr: sum += i
arr_list[n] = sum
def Thread_func(number):
arr_thread = []
start = perf_counter()
for i in range(number):
head = int(i*1000000/number)
tail = int((i+1)*1000000/number)
thread = threading.Thread(target=Result, args=(randnums[head:tail],i))
arr_thread.append(thread)
thread.start()
for thread in arr_thread:
thread.join()
end = perf_counter()
print('Multithread with {} threads fishined in {} s'.format(number, end - start))
random_arr = [random.choice(range(1000000)) for _ in range(1000000)]
arr = [2,3,4,5,6,7,8,9,10]
arr_list = list(range(max(arr)))
for i in arr:
Thread_func(i)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的系统上有很多流程。系统上任何内容的运行时间都受内核安排能力的影响,这取决于系统上其他所有内容的活动。获得一致运行时间的唯一方法是使用某种实时内核(或不运行多个进程的嵌入式设备)。
使用多个线程很少会为您提供CPU密集型任务的性能优势。由于Python的设计,实际上只有一个线程同时执行。当您尝试并行化IO-BOND操作(例如,通过网络获取数据)时,线程最有用。
对于CPU密集型操作,
多处理
模块通常是一个更好的选择(因为这会产生Python解释器的多个独立实例,并且可以利用多个CPU),但是在设置和撕裂中涉及的间接费用根据您的工作负载,这些过程使得解决方案没有比单个过程版本更好(甚至更糟)。There are a lot of processes running on your system. The runtime of anything on your system is influenced by the kernel's ability to schedule it, which depends on the activity of everything else on the system. The only way to get consistent runtimes is using some sort of realtime kernel (or embedded devices that don't run multiple processes).
Using multiple threads will seldom give you a performance advantage for CPU-intensive tasks. Due to the design of Python, only one thread is ever actually executing at the same time. Threads are most useful when you are trying to parallelize IO-bound operations (e.g., fetching data over the network).
For CPU-intensive operations, the
multiprocessing
module is often a better choice (because this spawns multiple independent instances of the Python interpreter and can take advantage of multiple CPUs), but the overhead involved in setting up and tearing down the processes make the solution no better (or even worse) than the single process version depending on your workload.