Python High Performance programming

Source: Internet
Author: User

1. Serial
Import Timeimport requestsurl_lists = [    ' http://www.baidu.com ',    ' http://fanyi.baidu.com ',    '/http ' Map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' Http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' http://news.baidu.com ',    ' HTTP// Xueshu.baidu.com ']start_time = time.time () for URL in url_lists:    response = requests.get (URL)    print ( Response.text) Print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:1.95

  

2. Multi-process
Import timeimport requestsfrom multiprocessing Import processurl_lists = [    ' http://www.baidu.com ',    '/HTTP/ Fanyi.baidu.com ',    ' http://map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' HTTP// News.baidu.com ',    ' http://xueshu.baidu.com ']def task (URL):    response = requests.get (URL)    print ( Response.text) If __name__ = = ' __main__ ':    p_list = []    start_time = Time.time () for the    URL in url_lists:        p = Process (Target=task, args= (URL,))        P_list.append (p)        P.start () for    p in p_list:        p.join ()    Print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:1.91

 

3. Process Pool (1)
Import timeimport requestsfrom concurrent.futures import Processpoolexecutor "" "Py2    without thread pool   but with process pool Py3    The thread pool     has process pools "" "url_lists = [    ' http://www.baidu.com ',    ' http://fanyi.baidu.com ', '/    http/ Map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' Http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' http://news.baidu.com ',    ' HTTP// Xueshu.baidu.com ']def task (URL):    response = requests.get (URL)    print (response.content) if __name__ = = ' __ Main__ ':    start_time = time.time ()    pool = Processpoolexecutor (Ten) for    URLs in url_lists:        Pool.submit (Task,url)    Pool.shutdown (wait=true)    print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:2.00

  

3. Process Pool (2)
Import Timeimport requestsfrom  multiprocessing Import poolurl_lists = [    ' http://www.baidu.com ',    '/HTTP/ Fanyi.baidu.com ',    ' http://map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' HTTP// News.baidu.com ',    ' http://xueshu.baidu.com ']def task (URL):    response = requests.get (URL)    return Response.contentdef Callbackfunc (content):    print (content) if __name__ = = ' __main__ ':    start_time = Time.time ()    pool = Pool (Ten)    for URLs in url_lists:        pool.apply_async (func=task,args= (URL,), callback= Callbackfunc)    pool.close ()    pool.join ()    print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:1.96

  

4. Multithreading
Import timeimport requestsfrom Threading Import threadurl_lists = [    ' http://www.baidu.com ',    '/HTTP/ Fanyi.baidu.com ',    ' http://map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' HTTP// News.baidu.com ',    ' http://xueshu.baidu.com ']def task (URL):    response = requests.get (URL)    print ( Response.text) If __name__ = = ' __main__ ':    t_list = []    start_time = Time.time () for the    URL in url_lists:        t = Thread (target=task, args= (URL,))        t_list.append (t)        T.start () for    T in T_list:        t.join ()    Print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:0.49

  

5. Thread pool
Import timeimport requestsfrom concurrent.futures import Threadpoolexecutor "" "Py2    without thread pool   but with process pool Py3    The thread pool     has process pools "" "url_lists = [    ' http://www.baidu.com ',    ' http://fanyi.baidu.com ', '/    http/ Map.baidu.com ',    ' http://music.baidu.com/',    ' http://tieba.baidu.com ',    ' http://v.baidu.com ',    ' Http://image.baidu.com ',    ' http://zhidao.baidu.com ',    ' http://news.baidu.com ',    ' HTTP// Xueshu.baidu.com ']def task (URL):    response = requests.get (URL)    print (response.content) if __name__ = = ' __ Main__ ':    start_time = time.time ()    pool = Threadpoolexecutor (Ten) for    URLs in url_lists:        pool.submit (Task,url)    Pool.shutdown (wait=true)    print ("Runtime: {}". Format (Time.time ()-start_time)) # runtime:0.51

  

Python High Performance programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.