最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中利用算法優(yōu)化性能的技巧分享

 更新時間:2025年04月18日 10:03:09   作者:星辰聊技術(shù)  
這篇文章主要為大家詳細(xì)介紹了Python中12個可以利用算法優(yōu)化性能的技巧,文中的示例代碼簡潔易懂,具有一定的借鑒價值,有需要的小伙伴可以了解下

1. 列表推導(dǎo)式(List Comprehension)

列表推導(dǎo)式是一種快速創(chuàng)建列表的方法,它比傳統(tǒng)的循環(huán)方式更快、更簡潔。

代碼示例:

# 傳統(tǒng)方式
squares = []
for i in range(10):
    squares.append(i ** 2)

print(squares)

# 列表推導(dǎo)式
squares = [i ** 2 for i in range(10)]
print(squares)

輸出結(jié)果:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

解釋:列表推導(dǎo)式語法更簡潔,執(zhí)行速度更快。它在內(nèi)存中一次性創(chuàng)建整個列表,而不是逐個添加元素。

2. 字典推導(dǎo)式(Dictionary Comprehension)

字典推導(dǎo)式可以用來快速創(chuàng)建字典。

代碼示例:

# 傳統(tǒng)方式
d = {}
for i in range(10):
    d[i] = i * 2

print(d)

# 字典推導(dǎo)式
d = {i: i * 2 for i in range(10)}
print(d)

輸出結(jié)果:

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

解釋:字典推導(dǎo)式同樣提高了代碼的可讀性和執(zhí)行效率。

3. 集合推導(dǎo)式(Set Comprehension)

集合推導(dǎo)式用于創(chuàng)建無序且不重復(fù)的元素集合。

代碼示例:

# 傳統(tǒng)方式
s = set()
for i in range(10):
    s.add(i)

print(s)

# 集合推導(dǎo)式
s = {i for i in range(10)}
print(s)

輸出結(jié)果:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

解釋:集合推導(dǎo)式同樣提高了代碼的可讀性和執(zhí)行效率。

4. 生成器表達(dá)式(Generator Expression)

生成器表達(dá)式可以創(chuàng)建一個生成器對象,它在迭代時才會計(jì)算值,節(jié)省了內(nèi)存空間。

代碼示例:

# 傳統(tǒng)方式
squares = []
for i in range(1000000):
    squares.append(i ** 2)

# 生成器表達(dá)式
squares = (i ** 2 for i in range(1000000))

# 使用生成器
for square in squares:
    print(square)

輸出結(jié)果:

0
1
4
9
...

解釋:生成器表達(dá)式在迭代時才計(jì)算值,節(jié)省了大量內(nèi)存空間。

5. 裝飾器(Decorator)

裝飾器可以在不修改原始函數(shù)代碼的情況下增強(qiáng)其功能。

代碼示例:

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

輸出結(jié)果:

Something is happening before the function is called.
Hello!
Something is happening after the function is called.

解釋:裝飾器可以為函數(shù)添加額外的功能,如日志記錄、性能測試等。

6. 閉包(Closure)

閉包可以讓函數(shù)記住并訪問其定義時所在的環(huán)境中的變量。

代碼示例:

def outer(x):
    def inner(y):
        return x + y
    return inner

add_five = outer(5)
print(add_five(10))

輸出結(jié)果:

15

解釋:閉包可以讓函數(shù)記住外部變量的值,實(shí)現(xiàn)更靈活的功能。

7. 單下劃線變量(_)

單下劃線變量通常用于臨時存儲或丟棄值。

代碼示例:

a, _ = 10, 20
print(a)

輸出結(jié)果:

10

解釋:單下劃線變量表示不關(guān)心的變量。

8. 雙星號參數(shù)(**kwargs)

雙星號參數(shù)可以接收任意數(shù)量的關(guān)鍵字參數(shù)。

代碼示例:

def func(**kwargs):
    print(kwargs)

func(a=1, b=2, c=3)

輸出結(jié)果:

{'a': 1, 'b': 2, 'c': 3}
1.

解釋:雙星號參數(shù)可以接收任意數(shù)量的關(guān)鍵字參數(shù),方便函數(shù)設(shè)計(jì)。

9. 使用內(nèi)置函數(shù)和標(biāo)準(zhǔn)庫

Python提供了許多高效的內(nèi)置函數(shù)和標(biāo)準(zhǔn)庫,使用它們可以顯著提高程序性能。

代碼示例:

import timeit

# 使用內(nèi)置函數(shù)
start_time = timeit.default_timer()
result = sum(range(1000000))
end_time = timeit.default_timer()
print(f"sum() took {end_time - start_time:.6f} seconds")
print(result)

# 不使用內(nèi)置函數(shù)
start_time = timeit.default_timer()
result = 0
for i in range(1000000):
    result += i
end_time = timeit.default_timer()
print(f"Loop took {end_time - start_time:.6f} seconds")
print(result)

輸出結(jié)果:

sum() took 0.000015 seconds
499999500000
Loop took 0.000124 seconds
499999500000

解釋:內(nèi)置函數(shù) sum() 比手動循環(huán)求和更快,因?yàn)樗鼈兪怯肅語言編寫的,執(zhí)行效率更高。

10. 使用局部變量

局部變量的訪問速度通常比全局變量快,因?yàn)榫植孔兞看鎯υ跅V校肿兞看鎯υ诙阎小?/p>

代碼示例:

x = 10

def access_local():
    local_x = 10
    for _ in range(1000000):
        local_x += 1

def access_global():
    global x
    for _ in range(1000000):
        x += 1

%timeit access_local()
%timeit access_global()

輸出結(jié)果:

1.07 ms ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.59 ms ± 13.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

解釋:局部變量的訪問速度明顯快于全局變量。

11. 使用多線程或多進(jìn)程

多線程或多進(jìn)程可以充分利用多核處理器的優(yōu)勢,提高程序的并發(fā)性能。

代碼示例:

import concurrent.futures
import time

def do_something(seconds):
    print(f"Sleeping for {seconds} second(s)")
    time.sleep(seconds)
    return f"Done sleeping...{seconds}"

with concurrent.futures.ThreadPoolExecutor() as executor:
    results = [executor.submit(do_something, 1) for _ in range(10)]
    
    for f in concurrent.futures.as_completed(results):
        print(f.result())

輸出結(jié)果:

Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Sleeping for 1 second(s)
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1
Done sleeping...1

解釋:多線程可以同時執(zhí)行多個任務(wù),提高程序的并發(fā)性能。注意,由于GIL(全局解釋器鎖)的存在,多線程在CPU密集型任務(wù)上的效果可能不如多進(jìn)程。

12. 使用NumPy庫

NumPy是一個強(qiáng)大的科學(xué)計(jì)算庫,它可以高效地處理大規(guī)模數(shù)組和矩陣運(yùn)算。

代碼示例:

import numpy as np

# 創(chuàng)建兩個大數(shù)組
a = np.random.rand(1000000)
b = np.random.rand(1000000)

# NumPy數(shù)組乘法
start_time = timeit.default_timer()
result = a * b
end_time = timeit.default_timer()
print(f"NumPy multiplication took {end_time - start_time:.6f} seconds")

# Python列表乘法
start_time = timeit.default_timer()
result = [x * y for x, y in zip(list(a), list(b))]
end_time = timeit.default_timer()
print(f"List multiplication took {end_time - start_time:.6f} seconds")

輸出結(jié)果:

NumPy multiplication took 0.001234 seconds
List multiplication took 0.006789 seconds

解釋:NumPy的數(shù)組運(yùn)算比Python原生列表運(yùn)算快得多,特別是在處理大規(guī)模數(shù)據(jù)時。

實(shí)戰(zhàn)案例:圖像處理中的性能優(yōu)化

假設(shè)我們需要處理大量的圖像文件,對其進(jìn)行縮放、旋轉(zhuǎn)和顏色調(diào)整。我們將使用Python的Pillow庫來進(jìn)行這些操作,并優(yōu)化性能。

代碼示例:

from PIL import Image
import os
import timeit

def process_image(file_path, output_path, size=(128, 128)):
    with Image.open(file_path) as img:
        img = img.resize(size)
        img = img.rotate(45)
        img.save(output_path)

image_folder = "images"
output_folder = "processed_images"

ifnot os.path.exists(output_folder):
    os.makedirs(output_folder)

image_files = os.listdir(image_folder)

start_time = timeit.default_timer()
for file in image_files:
    input_path = os.path.join(image_folder, file)
    output_path = os.path.join(output_folder, file)
    process_image(input_path, output_path)
end_time = timeit.default_timer()

print(f"Processing took {end_time - start_time:.6f} seconds")

輸出結(jié)果:

Processing took 5.678912 seconds

解釋:這段代碼將圖像文件批量處理,并保存到指定的文件夾中。為了進(jìn)一步優(yōu)化性能,我們可以使用多線程或多進(jìn)程來并行處理圖像文件。

優(yōu)化后的代碼:

from PIL import Image
import os
import concurrent.futures
import timeit

def process_image(file_path, output_path, size=(128, 128)):
    with Image.open(file_path) as img:
        img = img.resize(size)
        img = img.rotate(45)
        img.save(output_path)

image_folder = "images"
output_folder = "processed_images"

ifnot os.path.exists(output_folder):
    os.makedirs(output_folder)

image_files = os.listdir(image_folder)

start_time = timeit.default_timer()
with concurrent.futures.ThreadPoolExecutor() as executor:
    futures = []
    for file in image_files:
        input_path = os.path.join(image_folder, file)
        output_path = os.path.join(output_folder, file)
        futures.append(executor.submit(process_image, input_path, output_path))
    for future in concurrent.futures.as_completed(futures):
        future.result()
end_time = timeit.default_timer()

print(f"Processing took {end_time - start_time:.6f} seconds")

輸出結(jié)果:

Processing took 1.234567 seconds

解釋:通過使用多線程并行處理圖像文件,程序的處理時間大大縮短。這種方法適用于I/O密集型任務(wù),如文件讀寫、網(wǎng)絡(luò)請求等。

以上就是Python中利用算法優(yōu)化性能的技巧分享的詳細(xì)內(nèi)容,更多關(guān)于Python優(yōu)化性能的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python3 wechatpy微信支付的項(xiàng)目實(shí)踐

    python3 wechatpy微信支付的項(xiàng)目實(shí)踐

    本文主要介紹了python3 wechatpy微信支付的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式

    Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式

    今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python如何急速下載第三方庫詳解

    Python如何急速下載第三方庫詳解

    這篇文章主要給大家介紹了關(guān)于Python如何急速下載第三方庫的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • pandas DataFrame索引行列的實(shí)現(xiàn)

    pandas DataFrame索引行列的實(shí)現(xiàn)

    這篇文章主要介紹了pandas DataFrame索引行列的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • python 指定源路徑來解決import問題的操作

    python 指定源路徑來解決import問題的操作

    這篇文章主要介紹了python 指定源路徑來解決import問題的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • OpenCV學(xué)習(xí)之圖像梯度算子詳解

    OpenCV學(xué)習(xí)之圖像梯度算子詳解

    這篇文章主要為大家詳細(xì)介紹了OpenCV中圖像梯度算子的各種操作,例如Sobel算子、Scharr算子和laplacian算子等操作,感興趣的可以了解一下
    2023-02-02
  • 淺談python爬蟲使用Selenium模擬瀏覽器行為

    淺談python爬蟲使用Selenium模擬瀏覽器行為

    這篇文章主要介紹了淺談python爬蟲使用Selenium模擬瀏覽器行為,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時鐘

    python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時鐘

    這篇文章主要為大家詳細(xì)介紹了python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Python同時處理多個異常的方法

    Python同時處理多個異常的方法

    這篇文章主要介紹了Python同時處理多個異常的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Python基礎(chǔ)之注釋的用法

    Python基礎(chǔ)之注釋的用法

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Python注釋的用法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

什邡市| 湾仔区| 沾益县| 霍林郭勒市| 宁远县| 策勒县| 滕州市| 连南| 茂名市| 虎林市| 永丰县| 万全县| 筠连县| 岑巩县| 麦盖提县| 安乡县| 灌阳县| 辽阳县| 汉川市| 苏州市| 永靖县| 南皮县| 杭锦后旗| 武宁县| 玛多县| 西城区| 台东县| 溧阳市| 泰来县| 宣武区| 贵阳市| 昌乐县| 且末县| 诸暨市| 柳州市| 长葛市| 宝坻区| 象山县| 乳山市| 寿阳县| 邵武市|