python多线程耗时大_python多线程用法及与单线程耗时比较

下面,通过一个简单的例子,来把多线程和单线程执行任务的耗时做个比较

import time

import threading

# 音乐播放器

def music(func, loop):

for i in range(loop):

print("I was listening to %s the %d time! %s" % (func, i+1, time.time()))

time.sleep(2)

# 视频播放器

def movie(func, loop):

for i in range(loop):

print("I was watching %s the %d time! %s" % (func, i+1, time.time()))

time.sleep(5)

# 创建线程数组

threads = []

# 创建线程t1,并添加到线程数组

t1 = threading.Thread(target=music, args=('十年',2)) # 已经创建好的一个线程

threads.append(t1) # 追加到线程数组

# 创建线程t2,并添加到线程数组

t2 = threading.Thread(target=movie, args=('疯狂动物城', 2)) # args是一个元组

threads.append(t2)

# 创建线程t3,并添加到线程数组

# t3 = threading.Thread(target=movie, args=('少林足球', 2))

# th

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值