- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
asynchronous slower than synchronous 的答案没有涵盖我正在处理的场景,因此出现了这个问题。
我在 Windows 10 上使用 Python 3.6.0 读取 11 个相同的 JSON 文件,名称为 k80.json
到 k90.json
,每个 18.1 MB。
首先,我尝试同步、顺序读取所有 11 个文件。完成需要 5.07s。
from json import load
from os.path import join
from time import time
def read_config(fname):
json_fp = open(fname)
json_data = load(json_fp)
json_fp.close()
return len(json_data)
if __name__ == '__main__':
NUM_THREADS = 12
idx = 0
in_files = [join('C:\\', 'Users', 'userA', 'Documents', f'k{idx}.json') for idx in range(80, 91)]
print('Starting sequential run.')
start_time1 = time()
for fname in in_files:
print(f'Reading file: {fname}')
print(f'The JSON file size is {read_config(fname)}')
read_duration1 = round(time() - start_time1, 2)
print('Ending sequential run.')
print(f'Synchoronous reading took {read_duration1}s')
print('\n' * 3)
结果
Starting sequential run.
Reading file: C:\Users\userA\Documents\k80.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k81.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k82.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k83.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k84.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k85.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k86.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k87.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k88.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k89.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k90.json
The JSON file size is 5
Ending sequential run.
Synchoronous reading took 5.07s
接下来,我尝试使用带有 map
函数调用的 ThreadPoolExecutor
运行它,使用 12 个线程。这花费了 5.69s。
from concurrent.futures import ThreadPoolExecutor
from json import load
from os.path import join
from time import time
def read_config(fname):
json_fp = open(fname)
json_data = load(json_fp)
json_fp.close()
return len(json_data)
if __name__ == '__main__':
NUM_THREADS = 12
idx = 0
in_files = [join('C:\\', 'Users', 'userA', 'Documents', f'k{idx}.json') for idx in range(80, 91)]
th_pool = ThreadPoolExecutor(max_workers=NUM_THREADS)
print(f'Starting mapped pre-emptive threaded pool run with {NUM_THREADS} threads.')
start_time2 = time()
with th_pool:
map_iter = th_pool.map(read_config, in_files, timeout=10)
read_duration2 = round(time() - start_time2, 2)
print('The JSON file size is ')
map_results = list(map_iter)
for map_res in map_results:
print(f'The JSON file size is {map_res}')
print('Ending mapped pre-emptive threaded pool run.')
print(f'Mapped asynchoronous pre-emptive threaded pool reading took {read_duration2}s')
print('\n' * 3)
结果
Starting mapped pre-emptive threaded pool run with 12 threads.
The JSON file size is
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
Ending mapped pre-emptive threaded pool run.
Mapped asynchoronous pre-emptive threaded pool reading took 5.69s
最后,我尝试使用带有 submit
函数调用的 ThreadPoolExecutor
运行它,使用 12 个线程。这花费了 5.73s。
from concurrent.futures import ThreadPoolExecutor
from json import load
from os.path import join
from time import time
def read_config(fname):
json_fp = open(fname)
json_data = load(json_fp)
json_fp.close()
return len(json_data)
if __name__ == '__main__':
NUM_THREADS = 12
idx = 0
in_files = [join('C:\\', 'Users', 'userA', 'Documents', f'k{idx}.json') for idx in range(80, 91)]
th_pool = ThreadPoolExecutor(max_workers=NUM_THREADS)
results = []
print(f'Starting submitted pre-emptive threaded pool run with {NUM_THREADS} threads.')
start_time3 = time()
with th_pool:
for fname in in_files:
results.append(th_pool.submit(read_config, fname))
read_duration3 = round(time() - start_time3, 2)
for result in results:
print(f'The JSON file size is {result.result(timeout=10)}')
print('Ending submitted pre-emptive threaded pool run.')
print(f'Submitted asynchoronous pre-emptive threaded pool reading took {read_duration3}s')
结果
Starting submitted pre-emptive threaded pool run with 12 threads.
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
Ending submitted pre-emptive threaded pool run.
Submitted asynchoronous pre-emptive threaded pool reading took 5.73s
问题
在读取像这样相当大的 JSON 文件时,为什么同步读取比线程读取执行得更快?鉴于文件大小和正在读取的文件数量,我期望线程处理速度更快。
是否需要比这些文件大得多的 JSON 文件才能使线程处理的性能优于同步读取?如果不是,还有哪些其他因素需要考虑?
提前感谢您抽出时间提供帮助。
后脚本
感谢下面的回答,我稍微更改了 read_config
方法以引入 3s sleep 延迟(模拟 IO-wait 操作),现在线程版本真的很出色(38.81s 对比 9.36s 和 9.39s)。
def read_config(fname):
json_fp = open(fname)
json_data = load(json_fp)
json_fp.close()
sleep(3) # Simulate an activity that waits on I/O.
return len(json_data)
结果
Starting sequential run.
Reading file: C:\Users\userA\Documents\k80.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k81.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k82.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k83.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k84.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k85.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k86.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k87.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k88.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k89.json
The JSON file size is 5
Reading file: C:\Users\userA\Documents\k90.json
The JSON file size is 5
Ending sequential run.
Synchoronous reading took 38.81s
Starting mapped pre-emptive threaded pool run with 12 threads.
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
Ending mapped pre-emptive threaded pool run.
Mapped asynchoronous pre-emptive threaded pool reading took 9.36s
Starting submitted pre-emptive threaded pool run with 12 threads.
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
The JSON file size is 5
Ending submitted pre-emptive threaded pool run.
Submitted asynchoronous pre-emptive threaded pool reading took 9.39s
最佳答案
我不是专家,但总的来说,要使线程对速度有用,您的程序需要等待 IO。线程不会让您访问并行 CPU 线程,它只是允许操作并行运行,共享相同的 CPU 时间和 Python 解释器(如果您想访问更多 CPU,您应该查看 ProcessPoolExecutor)
例如,如果您从多个远程数据库而不是本地文件读取数据,那么您的程序将有很多时间在等待 IO 而没有使用本地资源。在这种情况下,线程可能会有所帮助,因为您可以并行等待,或者在等待另一个项目的同时处理一个项目。但是,由于您的所有数据都来自本地文件,您可能已经最大化了本地磁盘 IO,因此您无法同时读取多个文件(或者至少不能比顺序读取它们的速度快)。您的机器仍然必须使用相同的资源完成所有相同的任务,它在这两种变体中实际上没有任何“停机时间”,这就是为什么它们花费几乎相同的时间。
关于python - 在中等大小的 JSON 文件上使用线程池同步读取比异步读取更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58088038/
好吧,我知道这个问题已经被问了无数次了。但是,对于我在谷歌搜索中似乎无法找到的问题,我还有一个小补充。 我当然不是 FFMPEG 的专家……我一直在使用 FFMPEG 的标准加速/减速模板,我正在使用
考虑这三个文档... [ { _id: "...", _rev: "...", title: "Foo", body: "...
我想知道访问我的全局变量的最快方法...它们只会在 Beta 测试阶段发生变化。在我们上线之前。从那时起,它们将永远不会改变。 我认为从 web.config 中获取内容会产生开销,而且编写 App.
这个问题在这里已经有了答案: 11 年前关闭。 Possible Duplicate: Is there a performance difference between BETWEEN and IN
我很想知道对通常作为查询目标的数字列进行分区是否有性能优势。目前我有一个包含约 5000 万条记录的物化 View 。当使用常规 b 树索引并按此数字列搜索时,我得到的成本为 7,查询结果大约需要 0
我需要编写一个库,它执行许多远程 HTTP 调用来获取内容。我可以按照描述做here ,但是有没有更好的方法(在性能方面)如何做到这一点?如果我按照示例中所述进行操作,我总是会创建一个 URL 对象,
该代码非常不言自明。只是有很多我需要独立随机化的范围。例如,范围('W1:W4')不应与范围('W5:W8')混淆,因此我不能只是随机化范围('W1:W80')。任何帮助或建议都会很棒!多谢。目前,代
我正在使用 ADT 模拟器。我在我的模拟器中使用默认的 Android 虚拟设备。我创建了一个版本 4.0.3。 问题 太慢了。有时我在尝试更改 fragment 时会收到加载点击。 我使用的代码是有
我正在尝试获取一个包含三个表中的信息的数组。结果应该是一个数组,我可以在其中循环遍历第一个表、第二个表中的相关行以及第三个表到第二个表中的相关行。目前,我有三个独立的 SQL 查询,然后将它们重组为一
我已经学会了两种在服务器上上传图像的方法(可能还有更多..)。 1) 创建 NSData 并将其添加到请求正文中 2)创建字节数组并像简单数组一样以json形式发送 1) 创建 NSData 并将其添
我有一个 UItextview,我可以在里面写入数据类,我可以在我的 View 中的任何地方提供数据,在 ViewDidAppear 函数中我传递了我的数据,但它有点慢。文本在 0.2-0.3 秒后出
如何为 discoverAllContactUserInfosWithCompletionHandler 创建优先级高于默认值的 CKOperation? 我找不到不使用 [[CKContainer
我在 unix 模块下编写了一个内核级函数,用于对系统负载进行采样。我在 clock.c 下的 clock() 中调用示例函数,以在每个时钟(例如,我的系统上每 10 毫秒)拍摄系统负载的快照。有没有
我正在制作一个应用程序,该应用程序将根据变量的值使用鼠标/键盘(宏)模拟操作。 这里有我制作的 de 扫描代码: void ReadMemory(int value){ DWORD p
我想知道在计算上调用嵌套在对象中的函数的最快方法是什么,所以我做了一个快速的 jsPerf.com 基准测试,其中我考虑了三种可能性——从数组中调用函数,从“核心”中调用函数对象和函数对象: var
我用 php 做了一个图像缩放器。调整图像大小时,它会缓存一个具有新尺寸的新 jpg 文件。下次您调用确切的 img.php?file=hello.jpg&size=400 时,它会检查是否已经创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Which is best for data store Struct/Classes? 考虑我有一个 Em
我正在尝试为多组列自动计算每行的平均分数。例如。一组列可以代表不同比例的项目。这些列也被系统地命名 (scale_itemnumber)。 例如,下面的虚拟数据框包含来自三个不同比例的项目。(可能会出
所以我知道散列图使用桶和散列码等等。根据我的经验,Java 哈希码并不小,但通常很大,所以我假设它没有在内部建立索引。除非哈希码质量很差导致桶长度和桶数量大致相等,否则 HashMap 比名称-> 值
假设我有一个非常缓慢和大的 for 循环。 如何将其拆分为多个线程以使其运行速度更快? for (int a = 0; a { slowMet
我是一名优秀的程序员,十分优秀!