python读什么文件最快的软件_在Python中重读文件的最快方法是什么?

在我看来,你的问题不在于重读文件,而在于将长列表的切片与短列表相匹配。正如其他答案所指出的,你可以使用普通列表或内存映射文件来加速你的程序。在

如果您希望使用特定的数据结构来进一步提高速度,那么我建议您研究一下blist,特别是因为它在切片列表方面比标准Python列表有更好的性能:它们声明O(logn),而不是O(n)。在

在10MB的列表中,我测量到了大约4倍的加速:import random

from blist import blist

LINE_NUMBER = 1000000

def write_files(line_length=LINE_NUMBER):

with open('haystack.txt', 'w') as infile:

for _ in range(line_length):

infile.write('an example\n')

with open('needles.txt', 'w') as infile:

for _ in range(line_length / 100):

first_rand = random.randint(0, line_length)

second_rand = random.randint(first_rand, line_length)

needle = random.choice(['an example', 'a sample'])

infile.write('%s\t%s\t%s\n' % (needle, first_rand, second_rand))

def read_files():

with open('haystack.txt', 'r') as infile:

normal_list = []

for line in infile:

normal_list.append(line.strip())

enhanced_list = blist(normal_list)

return normal_list, enhanced_list

def match_over(list_structure):

matches = 0

total = len(list_structure)

with open('needles.txt', 'r') as infile:

for line in infile:

needle, start, end = line.split('\t')

start, end = int(start), int(end)

if needle in list_structure[start:end]:

matches += 1

return float(matches) / float(total)

通过IPython的%time命令测量,blist需要12秒,而普通的list需要46秒:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值