代码优化
运行time python 文件名.py可以查看运行时间
concurrent.futures 模块
标准方法
让我们举一个简单的例子,在单个文件夹中有一个图片数据集,其中有数万张图片。在这里,我们决定使用 1000 张。我们希望在所有图片被传递到深度神经网络之前将其调整为 600×600 像素分辨率的形式。以下是你经常会在 GitHub 上看到的标准 Python 代码:
import glob
import os
import cv2
### Loop through all jpg files in the current folder
### Resize each one to size 600x600
for image_filename in glob.glob("*.jpg"):
### Read in the image data
img = cv2.imread(image_filename)
### Resize the image
img = cv2.resize(img, (600, 600))
更快的方法
import glob
import os
import cv2
import concurrent.futures
def load_and_resize(image_filename):
### Read in the image data
img = cv2.imread(image_filename)
### Resize the image
img = cv2.resize(img, (600, 600))
### Create a pool of processes. By default, one is created for each CPU in your machine.
with concurrent.futures.ProcessPoolExecutor() as executor:
### Get a list of files to process
image_files = glob.glob("*.jpg")
### Process the list of files, but split the work across the process pool to use all CPUs
### Loop through all jpg files in the current folder
### Resize each one to size 600x600
executor.map(load_and_resize, image_files)
CPU 核越多,启动的 Python 进程越多
「executor.map()」将你想要运行的函数和列表作为输入,列表中的每个元素都是我们函数的单个输入。
Last updated
Was this helpful?