Python初学者:如何减少这个小程序的执行时间?

2024-09-24 22:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

import json
with open('data.json') as f:
    data=json.load(f)
lis= [row['text'] for row in data['City']['values'] if row['text'].startswith("N")]
lis=sorted(lis)
print lis[:5]

json数据如下:

{"City": {"values": [{"text": "Abee|Alberta|Canada", "state": "AB", "id": 21774}, {"text": "Acadia Valley|Alberta|Canada", "state": "AB", "id": 21775}, {"text": "Acme|Alberta|Canada", "state": "AB", "id": 21776}, {"text": "Airdrie|Alberta|Canada", "state": "AB", "id": 21777}, {"text": "Alderson|Alberta|Canada", "state": "AB", "id": 21778}, {"text": "Alix|Alberta|Canada", "state": "AB", "id": 21779}, {"text": "Alliance|Alberta|Canada", "state": "AB", "id": 21780}, {"text": "Andrew|Alberta|Canada", "state": "AB", "id": 21781}, {"text": "Ardmore|Alberta|Canada", "state": "AB", "id": 21782}, {"text": "Ardrossan|Alberta|Canada", "state": "AB", "id": 21783}, {"text": "Ashmont|Alberta|Canada", "state": "AB", "id": 21784}, {"text": "Athabasca|Alberta|Canada", "state": "AB", "id": 21785}, {"text": "Atikameg|Alberta|Canada", "state": "AB", "id": 21786}, {"text": "Atmore|Alberta|Canada", "state": "AB", "id": 21787}, {"text": "Avenir|Alberta|Canada", "state": "AB", "id": 21788}, {"text": "Balzac|Alberta|Canada", "state": "AB", "id": 21789}, {"text": "Banff|Alberta|Canada", "state": "AB", "id": 21790}, {"text": "Barons|Alberta|Canada", "state": "AB", "id": 21791}, {"text": "Barrhead|Alberta|Canada", "state": "AB", "id": 21792}, {"text": "Bashaw|Alberta|Canada", "state": "AB", "id": 21793}, {"text": "Bassano|Alberta|Canada", "state": "AB", "id": 21794}, {"text": "Beaumont|Alberta|Canada", "state": "AB", "id": 21795}, {"text": "Beaverlodge|Alberta|Canada", "state": "AB", "id": 21796}, {"text": "Beiseker|Alberta|Canada", "state": "AB", "id": 21797}, {"text": "Bellevue|Alberta|Canada", "state": "AB", "id": 21798}, {"text": "Bellis|Alberta|Canada", "state": "AB", "id": 21799}, {"text": "Benalto|Alberta|Canada", "state": "AB", "id": 21800}, {"text": "Bentley|Alberta|Canada", "state": "AB", "id": 21801}, {"text": "Bergen|Alberta|Canada", "state": "AB", "id": 21802}, {"text": "Berwyn|Alberta|Canada", "state": "AB", "id": 21803}, {"text": "Big Valley|Alberta|Canada", "state": "AB", "id": 21804}, {"text": "Bilby|Alberta|Canada", "state": "AB", "id": 21805}, {"text": "Bittern Lake|Alberta|Canada", "state": "AB", "id": 21806}, {"text": "Black Diamond|Alberta|Canada", "state": "AB", "id": 21807}, {"text": "Blackfalds|Alberta|Canada", "state": "AB", "id": 21808}, {"text": "Blackie|Alberta|Canada", "state": "AB", "id": 21809}, {"text": "Blairmore|Alberta|Canada", "state": "AB", "id": 21810}, {"text": "Blue Ridge|Alberta|Canada", "state": "AB", "id": 21811}, {"text": "Bluesky|Alberta|Canada", "state": "AB", "id": 21812}, {"text": "Bluffton|Alberta|Canada", "state": "AB", "id": 21813}, {"text": "Bon Accord|Alberta|Canada", "state": "AB", "id": 21814}, {"text": "Bonnyville|Alberta|Canada", "state": "AB", "id": 21815}, {"text": "Bowden|Alberta|Canada", "state": "AB", "id": 21816}, {"text": "Bow Island|Alberta|Canada", "state": "AB", "id": 21817}, {"text": "Boyle|Alberta|Canada", "state": "AB", "id": 21818}, {"text": "Brampton|Alberta|Canada", "state": "AB", "id": 21819}]}}

非常感谢您的帮助!你知道吗


Tags: textimportidjsoncitydataabwith
1条回答
网友
1楼 · 发布于 2024-09-24 22:23:58

实际上,这是一个按“N%”、排序、限制的查询筛选器。你知道吗

我真的会问自己,这将如何运行,我可以做什么工作,使它的时间敏感部分尽可能少的工作前期?你知道吗

你的情况很明显-数据集有变化吗?如果它不是每次运行,那么您应该将它准备到内存中(或者至少存储为非json的内容)。一旦您采用这种方法,就会有很多选项(比如将sqlite与内存中的db一起使用)。你知道吗

为了比较其他方法,让我们至少从加载的文件内容开始(因此我们不分析磁盘io)。你知道吗

with open('data.json') as f:
    data = f.read()

现在,您的方法(我们将删除打印位,因为在比较中没有太多的点分析):

def original(data):
    data = json.loads(data)
    lis = [row['text'] for row in data['City']['values'] if row['text'].startswith("A")]
    lis = sorted(lis)
    return lis[:5]

还有一个不同的例子,我们使用regex直接处理文本:

def with_regex(data):
    filtered = [x[9:-1] for x in re.findall('"text": "A[^"]+"', data)]
    return sorted(filtered)[:5]

现在比较一下:

%timeit original(data)
10000 loops, best of 3: 57.4 µs per loop

%timeit with_regex(data)
100000 loops, best of 3: 11.1 µs per loop

因此,在这种情况下,使用regex可以更快(5倍)完成这项工作,但数据需要格式良好。你知道吗

如果您对它进行分析,您将看到您的版本将所有时间都花在json解码器中。最好的办法就是让它消失(我只做一次)。你知道吗

相关问题 更多 >