不要再写低效的Python代码,一些小Tips

B站|公众号:啥都会一点的研究生

相关阅读

这七个100%提高Python代码性能的技巧,一定要知道
整理了几个100%会踩的Python细节坑,提前防止脑血栓
整理了十个100%提高效率的Python编程技巧,更上一层楼
Python-列表,从基础到进阶用法大总结,进来查漏补缺
Python-元组,从基础到进阶用法大总结及与列表核心区别,进来查漏补缺
Python-字典,从基础到进阶用法大总结,进来查漏补缺
Python-集合,从基础到进阶大总结,进来查漏补缺
这些包括我在内都有的Python编程陋习,趁早改掉
Python可变类型的坑,不要再踩了
列表推导式,Python中最好的特性?可读性?
元组啊,不就是不可变的列表吗?
订阅专栏 ===> Python

hello,本期将和大家一起继续拓展一些优化 Python 代码性能的有用技巧,一起看看吧

1. 使用 Python 调试器 (pdb)

Python 调试器允许单步执行代码并识别任何性能问题,可以使用它来查找瓶颈并优化代码

import pdb

def calculate_sum(n):
    total = 0
    for i in range(n):
        pdb.set_trace()
        total += i
    return total

calculate_sum(5)

该例展示了如何使用 Python 调试器 (pdb) 来优化函数。 pdb.set_trace() 语句放置在循环内以在每次迭代时进入调试器。 这使得能够在每次迭代时检查变量和代码流,真得很常用~

2. 使用内置函数与库

Python 提供了各种可以高效执行复杂任务的内置函数和库。 例如,使用 map() 将函数应用于列表中的每个元素,或使用 pandas 库来操作 DataFrame 中的数据

# Example of using map() function to apply a function to a list 
lst = [1, 2, 3, 4, 5] 
new_lst = list(map(lambda x: x * 2, lst))  

# Multiply each element in the list by 2 
print(new_lst) 
# Output: [2, 4, 6, 8, 10] 

3. 使用生成器

生成器是一种在 Python 中创建迭代器的高效节省内存方式。 通常用于迭代大型数据集而无需将它们存储在内存中

# Example of using a generator to iterate over a large dataset 
def read_file(filename): 
  with open(filename) as file: 
    for line in file: 
      yieldline.strip() 
        for line in read_file("data.txt"): 
          print(line)

该例定义了一个函数 read_file,它逐行读取文件并使用生成器在读取时生成每一行。 该函数使用 with 语句打开文件,以确保文件在完成后正确关闭。 当以文件名作为参数调用该函数时,它返回一个生成器对象。 然后 for 循环遍历生成器返回的每一行并将其打印到控制台。

4. 使用元组与字典推导

与列表推导类似

# Example of using set comprehension to create a new set 
lst = [1, 2, 3, 4, 5] 
new_set = {x * 2 for x in lst}  

# Multiply each element in the list by 2 and create a new set 
print(new_set)  
# Output: {2, 4, 6, 8, 10} 


# Example of using dictionary comprehension to create a new dictionary 
lst = [("apple", 2), ("banana", 3), ("orange", 4)] 
new_dict = {k: v for k, v in lst}  
# Convert the list of tuples to a dictionary 
print(new_dict)  
# {'apple': 2, 'banana': 3, 'orange': 4}

5. 使用最有效的数据结构

高效的数据结构可以通过为各种操作提供不同的时间复杂度来极大地提高 Python 代码的性能。 选择最佳数据结构会对代码速度产生重大影响

例如,对于较大的列表,使用列表实现队列数据结构可能会很慢,因为从列表的开头追加和弹出很慢。 相反,使用专为实现队列而设计的 collections.deque 对象可以显著提高性能

下例说明正确的数据结构如何在搜索数字列表的最大值和最小值时提高代码性能:

# Using a list to find max and min values 
numbers = [5, 10, 2, 8, 1] 
max_val = max(numbers) 
min_val = min(numbers) 

# Using a heap to find max and min values 
import heapq 
numbers = [5, 10, 2, 8, 1] 
max_val = heapq.nlargest(1, numbers)[0] 
min_val = heapq.nsmallest(1, numbers)[0]

6. 使用装饰器简化代码

关于装饰器之前有专门发过一篇,装饰器是 Python 中的一个强大工具,可以修改函数或类的行为。 可用于简化代码并提高其性能

装饰器还可用于简化代码中的错误处理。 例如,假设有一个可能引发异常的函数:

def potentially_error_prone_function():
    if some_condition:
        raise ValueError("Oops!")
    return "Success"

可以将此函数包装在 try/except 块中以处理异常:

def error_handling_wrapper(func):
    def wrapper():
        try:
            result = func()
        except Exception as e:
            print("An error occurred:", e)
            return None
        return result
    return wrapper

@error_handling_wrapper
def potentially_error_prone_function():
    if some_condition:
        raise ValueError("Oops!")
    return "Success"

如上,当调用 potentially_error_prone_function() 时,如果引发异常,它将被捕获并打印一条消息,而不是程序崩溃

7. 尽可能避免使用 try-except

是的,和上面不冲突,try-except 块会降低代码速度,可以改用条件语句来处理错误

# Bad Code
def divide(a, b):
    try:
        result = a / b
    except ZeroDivisionError:
        result = None
    return result

# Good Code
def divide(a, b):
    if b == 0:
        return None
    else:
        return a / b

该例使用 try-except 块来处理分母为零的情况。 此方法可能很慢且效率低下,尤其是当 try-except 块位于经常调用的函数中时。 另一方面,好的代码应该在执行除法前检查分母是否为零,这是处理零分母情况的一种更快、更有效的方法。

8. 使用Cython加速代码

Cython 是 Python 的超集,允许为 Python 编写 C 扩展,可以显著加快我们的代码速度,特别是对于计算密集型任务

下例是使用 Cython 加速计算平方和的 Python 函数的示例:

# Python Code
def calculate_sum(n):
    total = 0
    for i in range(n):
        total += i
    return total

# Cython Code
%%cython
def calculate_sum_cython(int n):
    cdef int total = 0
    for i in range(n):
        total += i
    return total

首先创建一个包含该函数的 Cython 版本的 .pyx 文件

# file: sum_squares_cython.pyx

def sum_squares_cython(int n):
    cdef int total = 0
    cdef int i
    for i in range(n):
        total += i**2
    return total

我们为变量添加了类型声明 (cdef int),这允许 Cython 生成更快的 C 代码

要将 Cython 代码编译成 Python 扩展模块,需要创建一个 setup.py 文件:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("sum_squares_cython.pyx"))

现在可以通过运行 python setup.py build_ext --inplace 来编译扩展模块。 这将创建一个名为 sum_squares_cython.so(或 Windows 上的 sum_squares_cython.pyd)的文件

最后,可以在 Python 中导入并使用 sum_squares_cython 函数:

from sum_squares_cython import sum_squares_cython

result = sum_squares_cython(1000000)
print(result)

以上就是本期全部内容,下次再见~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啥都生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值