python每日一题:比较单线程,多线程,协程的运行效率

 1.普通串行运行程序:

     

import gevent,time
import urllib.request


def f1(url):
    print('start open ',url)
    a=urllib.request.urlopen(url)
    data=a.read()
    print(url,len(data))


if __name__=='__main__':
    time1=time.time()
    a=['http://www.baidu.com','http://www.hanwei.com','http://www.xiaomi.com','http://www.huawei.com','http://www.cnblogs.com']
    for i in a:
        f1(i)
    print('the time is:',time.time()-time1)

调试结果:

start open  http://www.baidu.com
http://www.baidu.com 153150
start open  http://www.hanwei.com
http://www.hanwei.com 26749
start open  http://www.xiaomi.com
http://www.xiaomi.com 324202
start open  http://www.huawei.com
http://www.huawei.com 132732
start open  http://www.cnblogs.com
http://www.cnblogs.com 47568
the time is: 16.556946992874146

2. 利用多线程:


import gevent,time,threading
import urllib.request
def f1(url):
    print('start open ',url)
    a=urllib.request.urlopen(url)
    data=a.read()
    print(url,len(data))
if __name__=='__main__':
    time1=time.time()
    a=['http://www.baidu.com','http://www.hanwei.com','http://www.xiaomi.com','http://www.huawei.com','http://www.cnblogs.com']
    t1 = threading.Thread(target=f1, args=(a[0],))
    t2 = threading.Thread(target=f1, args=(a[1],))
    t3 = threading.Thread(target=f1, args=(a[2],))
    t4 = threading.Thread(target=f1, args=(a[3],))
    t5 = threading.Thread(target=f1, args=(a[4],))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    t5.join()
    print('the time is:',time.time()-time1)


 

调试结果:

start open  http://www.baidu.com
start open  http://www.hanwei.com
start open  http://www.xiaomi.com
start open  http://www.huawei.com
start open  http://www.cnblogs.com
http://www.baidu.com 153101
http://www.hanwei.com 26749
http://www.xiaomi.com 324202
http://www.cnblogs.com 47650
http://www.huawei.com 132904
the time is: 9.23352837562561

3.使用协程进行处理:

import gevent,time
from gevent import  monkey; monkey.patch_all()
import urllib.request
def f1(url):
    print('starting open:',url)
    a=urllib.request.urlopen(url)
    data=a.read()
    print(url,len(data))

if __name__=='__main__':
    time1=time.time()
    a=['http://www.baidu.com','http://www.hanwei.com','http://www.xiaomi.com','http://www.huawei.com','http://www.cnblogs.com']
    s=[gevent.spawn(f1,i) for i in a]
    gevent.joinall(s)
    print('the time is:',time.time()-time1)

调试结果:

starting open: http://www.baidu.com
starting open: http://www.hanwei.com
starting open: http://www.xiaomi.com
starting open: http://www.huawei.com
starting open: http://www.cnblogs.com
http://www.baidu.com 153050
http://www.hanwei.com 26749
http://www.cnblogs.com 47568
http://www.xiaomi.com 324202
http://www.huawei.com 132904
the time is: 15.648895025253296

结论:看到网上说使用打开网页的例子进行比较线程的运行效率,这个例子是不大合适的,因为访问网站时,涉及到网站的响应问题,网速问题,比较响应时间不合理,但可以使用处理数据的速度进行对比,总体,采用多线程会快速一些,因为它是并发进行处理,而协程看似并发实则单线程进行处理,因此效率也是较低。但是协程的机制有利于我们在程序1运行过程中,处理一些其他特殊的事情程序2,而不必使用锁线程的方法;而且由于是用户级的线程调用,其占用内存等资源会较少,总体有一定的优势。

原文地址:https://www.cnblogs.com/xuehaiwuya0000/p/10241383.html