为什么每个线程运行时间不同

发布于 2025-02-12 05:40:11 字数 1157 浏览 2 评论 0原文

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)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2025-02-19 05:40:11

为什么每个线程运行时间都不同,并且每次运行代码时间都不同?线程的运行时间取决于什么?

您的系统上有很多流程。系统上任何内容的运行时间都受内核安排能力的影响,这取决于系统上其他所有内容的活动。获得一致运行时间的唯一方法是使用某种实时内核(或不运行多个进程的嵌入式设备)。

为什么顺序比多线程更快?

使用多个线程很少会为您提供CPU密集型任务的性能优势。由于Python的设计,实际上只有一个线程同时执行。当您尝试并行化IO-BOND操作(例如,通过网络获取数据)时,线程最有用。

对于CPU密集型操作,多处理模块通常是一个更好的选择(因为这会产生Python解释器的多个独立实例,并且可以利用多个CPU),但是在设置和撕裂中涉及的间接费用根据您的工作负载,这些过程使得解决方案没有比单个过程版本更好(甚至更糟)。

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 ?

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).

Why Sequential is faster than Multithread ?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文