我是靠谱客的博主 稳重小蝴蝶,最近开发中收集的这篇文章主要介绍python计算密集型效率对比,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

    • 一: 需求:
    • 二:验证
      • 1.1: 顺序计算
      • 1.2: 协程计算
      • 1.3: 多线程计算
    • 三:结论

一: 需求:

在进行大变量赋值计算的时候, 我发现之前人的代码, 使用了多线程。但是根据我的经验, 计算密集型, 效率一般遵循这样的规律:多进程 > 顺序运行 > 协程 > 多线程。 因此我感觉之前的写法效率不会高。

二:验证

  • 计算1~1000数字相加, 并打印结果。

1.1: 顺序计算

# -*- coding: utf-8 -*-
import time
import gevent
from gevent import monkey
from concurrent.futures import ThreadPoolExecutor

monkey.patch_all()
def a_and_b(ab):
    """两数相加"""
    a, b = ab
    return a + b

def normal_test():
    """顺序计算"""
    start_time = time.time()
    results = [a_and_b((i, i - 1)) for i in range(1, 1000)]

    for result in results:
        print(result)
    end_time = time.time()
    print("normal cost time is {}".format(end_time - start_time))


if __name__ == '__main__':
    normal_test()
  • normal cost time is 0.002415895462036133

1.2: 协程计算

# -*- coding: utf-8 -*-
import time
import gevent
from gevent import monkey
from concurrent.futures import ThreadPoolExecutor

monkey.patch_all()

def a_and_b(ab):
    """两数相加"""
    a, b = ab
    return a + b
    
def coroutine_test():
    """协程测试"""
    start_time = time.time()
    tasks = [gevent.spawn(a_and_b, (i, i - 1)) for i in range(1, 1000)]
    gevent.joinall(tasks)

    for task in tasks:
        print(task.value)
    end_time = time.time()
    print("coroutine cost time is {}".format(end_time - start_time))
    
if __name__ == '__main__':
    coroutine_test()
  • coroutine cost time is 0.010415792465209961

1.3: 多线程计算

# -*- coding: utf-8 -*-
import time
import gevent
from gevent import monkey
from concurrent.futures import ThreadPoolExecutor

monkey.patch_all()
def a_and_b(ab):
    """两数相加"""
    a, b = ab
    return a + b
    
def threading_test():
    """线程测试"""
    futures = []
    start_time = time.time()
    with ThreadPoolExecutor(max_workers=10) as executor:
        for i in range(1, 1000):
            futures.append(executor.submit(a_and_b, (i, i - 1)))

    for future in futures:
        print(future.result())

    end_time = time.time()
    print("threading cost time is {}".format(end_time - start_time))

if __name__ == '__main__':
    threading_test()
  • threading cost time is 0.07938933372497559

三:结论

  • 果然, 前人给埋的优化点被我发现了, 哈哈, 这个季度绩效有指望了。

最后

以上就是稳重小蝴蝶为你收集整理的python计算密集型效率对比的全部内容,希望文章能够帮你解决python计算密集型效率对比所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部