美文网首页
Golang协程与Python协程速度比较

Golang协程与Python协程速度比较

作者: 10xjzheng | 来源:发表于2019-10-07 15:18 被阅读0次

本实验通过抓取50页诗词并做对每一页里面的a标签对应的html页面(每页40个a标签)进行抓取,然后对html做简单解析,总共就是请求50+50*40 = 2050个页面,并解析这些页面的html内容。

1.Python 速度

总耗时:31.947秒 多次试验是在32s左右

image.png image.png image.png

源码:

from bs4 import BeautifulSoup
import time
import aiohttp
import asyncio

async def do_task(domain, pageUrl):
    async with aiohttp.ClientSession() as session:
        async with session.request('GET', pageUrl) as resp:
            if resp.status != 200:
                raise Exception('http error, url:{} code:{}'.format(pageUrl, resp.status))
            html = await resp.read()  # 可直接获取bytes
    soup = BeautifulSoup(html, 'html.parser')
    for h in soup.select('h3>a'):
        url = ''.join([domain, h.get('href')])
        async with aiohttp.ClientSession() as session:
            async with session.request('GET', url) as resp:
                if resp.status != 200:
                    raise Exception('http error, url:{} code:{}'.format(pageUrl, resp.status))
                html = await resp.read()  # 可直接获取bytes
        print('url:{} title:{}'.format(url, parse_text(html)))


def parse_text(html):
    soup = BeautifulSoup(html, 'html.parser')
    return str(soup.select('.shici-title')[0].get_text())


def main():
    domain = 'http://www.shicimingju.com'
    urlTemplate = domain + '/chaxun/zuozhe/9_{0}.html'
    pageNum = 50  # 读取50页诗词进行测试
    loop = asyncio.get_event_loop()  # 获取事件循环
    tasks = []
    for num in range(pageNum + 1):
        tasks.append(do_task(domain, urlTemplate.format(num + 1)))
    loop.run_until_complete(asyncio.wait(tasks))  # 协程
    loop.close()


if __name__ == '__main__':
    start = time.time()
    main()  # 调用方
    print('总耗时:%.3f秒' % float(time.time() - start))

2. Golang 速度

总耗时:15.366秒 多次试验基本是在15s左右,最快的几次甚至到了12s,13s,最差也是22s。

image.png image.png
image.png

源码:

package main

import (
    "fmt"
    "github.com/PuerkitoBio/goquery"
    "strconv"
    "strings"
    "sync"
    "time"
)

func do_task(url string, domain string) {
    p, err := goquery.NewDocument(url)
    if err != nil {
        panic(err)
    } else {
        p.Find("h3").Find("a").Each(func(i int, selection *goquery.Selection) {
            href, _ := selection.Attr("href")
            link := domain + href
            h, err := goquery.NewDocument(link)
            if err != nil {
                panic(err)
            } else {
                title := h.Find(".shici-title").Text()
                fmt.Printf("url:%s title:%s \n", link, title)
            }
        })
    }
    wg.Done()
}

var wg sync.WaitGroup

func main()  {
    start := time.Now().UnixNano()
    domain := "http://www.shicimingju.com"
    urlTemplate := domain + "/chaxun/zuozhe/9_{:num}.html"
    pageNum := 50
    wg.Add(50)
    for page := 1; page <= pageNum; page++ {
        url := strings.Replace(urlTemplate, "{:num}", strconv.Itoa(page), -1)
        go do_task(url, domain)
    }
    wg.Wait()
    end := time.Now().UnixNano()
    fmt.Printf("总耗时:%.3f秒 \n", float32(end - start)/1000000000)
}

3.结论

Golang的性能妥妥地要比Python,PHP好,毕竟是为并发而生的语言。

相关文章

  • Golang协程与Python协程速度比较

    本实验通过抓取50页诗词并做对每一页里面的a标签对应的html页面(每页40个a标签)进行抓取,然后对html做简...

  • python异步协程(aiohttp,asyncio)

    python异步协程 环境:python3.7.0 协程 协程,英文叫做 Coroutine,又称微线程,纤程,协...

  • python协程与golang协程的区别!

    最近经常有同学在小猿圈网站留言说:小猿圈网站上边的课程太多了,而且全部免费,自己就忍不住都想学习,尤其是学习了py...

  • 谁说Python协程是鸡肋的!站出来我不打死他!这么牛逼的协程!

    文章思路:本文将先介绍协程的概念,然后分别介绍Python2.x与3.x下协程的用法,最终将协程与多线程做比较并介...

  • Python 协程

    仅供学习,转载请注明出处 协程 协程,又称微线程,纤程。英文名Coroutine。 协程是啥 协程是python个...

  • 协程

    1.协程 协程,又称微线程,纤程。英文名Coroutine。 1.1 协程是什么 协程是python个中另外一种实...

  • 协程介绍

    协程 协程,又称微线程,纤程。英文名Coroutine。 1.协程是什么? 协程是python个中另外一种实现多任...

  • 4-7

    协程 协程,又称微线程,纤程。英文名Coroutine。 协程是啥 协程是python个中另外一种实现多任务的方式...

  • 协程

    协程 协程,又称微线程,纤程。英文名Coroutine。 协程是啥 协程是python个中另外一种实现多任务的方式...

  • 36.Python并发编程之协程

    Python并发编程之协程 协程协程是程序级别的概念,操作系统根本就没有协程的概念!!!协程的本质就是一条线程,可...

网友评论

      本文标题:Golang协程与Python协程速度比较

      本文链接:https://www.haomeiwen.com/subject/kujmpctx.html