[英]Optimize file and number line count in Python
我得到了一个包含许多文件夹,文件(.css,.py,.yml等)和代码行的python项目。 对于这个项目,我制作了一个名为“统计信息”的工具,可为我提供有关整个项目的信息,例如:
全球统计:
整个项目:: 32329行
项目主文件(.py,.yml):: 8420行
没有供应商零件的项目:: 1070行
核心(src目录):: 394行
与项目主文件相比的核心:: 5%Kraken Framework(vendor / *。py):: 7350行
主文件Python代码:: 93%
供应商Python代码:: 87%
整个项目规模:: 37M
为了获得所有这些数字,我主要使用两个函数:
def count_folder_lines(self, path):
files = glob.glob(path, recursive=True)
number = 0
for file in files:
num_lines = sum(1 for line in open(file))
number += num_lines
return number
和
def count_number_of_files(self, path):
files = glob.glob(path, recursive=True)
return len(files)
第一个用于计算文件夹中的行数,第二个用于计算特定文件的数量(例如:src / *。py)。 但是要获得项目的统计数据,需要花费4.9到5.3秒,这是很多时间。
有什么方法可以使其更快? 并行编程或使用Cython会改变某些东西吗?
祝你有美好的一天,谢谢。
终于找到了对我来说最有效的解决方案:我正在使用多处理模块来并行计算每个文件的行数。
def count_folder_lines(self, path):
"""
Use a buffer to count the number of line of each file among path.
:param path: string pattern of a file type
:return: number of lines in matching files
"""
files = glob.glob(path, recursive=True)
number = 0
for file in files:
f = open(file, 'rb')
bufgen = takewhile(lambda x: x,
(f.raw.read(1024 * 1024) for _ in repeat(None)))
number += sum(buf.count(b'\n') for buf in bufgen if buf)
return number
def count_number_of_files(self, path):
"""
Count number of files for a string pattern
:param path: files string pattern
:return: number of files matching the pattern
"""
files = glob.glob(path, recursive=True)
return len(files)
def multiproc(self):
"""
Multiprocessing to launch several processes to count number of
lines of each string pattern in self.files
:return: List of number of files per string pattern
(list of int).
"""
pool = mp.Pool()
asyncResult = pool.map_async(self.count_folder_lines, self.files)
return asyncResult.get()
使用此解决方案,计数所需的时间约为1.2s,而之前约为5s。
祝你有美好的一天!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.