南阳微网站制作,开发公司制度,购物网站后台管理模板,抖音引流推广软件我正在编写一个简单的应用程序#xff0c;它将大文本文件拆分为较小的文件#xff0c;并且我已经编写了2个版本#xff0c;一个使用列表#xff0c;另一个使用生成器。我使用memory_profiler模块对这两个版本进行了概要分析#xff0c;并清楚地显示了生成器版本的更好的内…我正在编写一个简单的应用程序它将大文本文件拆分为较小的文件并且我已经编写了2个版本一个使用列表另一个使用生成器。我使用memory_profiler模块对这两个版本进行了概要分析并清楚地显示了生成器版本的更好的内存效率但是在对使用生成器的版本进行概要分析时这很奇怪这增加了执行时间。下面的演示解释了我的意思
使用列表版本
frommemory_profilerimportprofileprofile()defmain():file_nameinput(Enter the full path of file you want to split into smaller inputFiles: )input_fileopen(file_name).readlines()num_lines_origlen(input_file)partsint(input(Enter the number of parts you want to split in: ))output_files[(file_namestr(i))foriinrange(1,parts1)]st0pint(num_lines_orig/parts)edpforiinrange(parts-1):withopen(output_files[i],w)asOF:OF.writelines(input_file[st:ed])sted
edstpwithopen(output_files[-1],w)asOF:OF.writelines(input_file[st:])if__name____main__:main()
与分析器一起运行时
$ time py36Splitting\ text\ files_BAD_usingLists.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3Filename:Splittingtext files_BAD_usingLists.pyLine# Mem usage Increment Line Contents647.8MiB0.0MiBprofile()7defmain():847.8MiB0.0MiBfile_nameinput(Enter the full path of file you want to split into smaller inputFiles: )9107.3MiB59.5MiBinput_fileopen(file_name).readlines()10107.3MiB0.0MiBnum_lines_origlen(input_file)11107.3MiB0.0MiBpartsint(input(Enter the number of parts you want to split in: ))12107.3MiB0.0MiBoutput_files[(file_namestr(i))foriinrange(1,parts1)]13107.3MiB0.0MiBst014107.3MiB0.0MiBpint(num_lines_orig/parts)15107.3MiB0.0MiBedp16108.1MiB0.7MiBforiinrange(parts-1):17107.6MiB-0.5MiBwithopen(output_files[i],w)asOF:18108.1MiB0.5MiBOF.writelines(input_file[st:ed])19108.1MiB0.0MiBsted20108.1MiB0.0MiBedstp2122108.1MiB0.0MiBwithopen(output_files[-1],w)asOF:23108.1MiB0.0MiBOF.writelines(input_file[st:])real0m6.115suser0m0.764ssys0m0.052s
在没有分析器的情况下运行
$ time py36Splitting\ text\ files_BAD_usingLists.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3real0m5.916suser0m0.696ssys0m0.080s
现在使用发电机
profile()defmain():file_nameinput(Enter the full path of file you want to split into smaller inputFiles: )input_fileopen(file_name)num_lines_origsum(1for_ininput_file)input_file.seek(0)partsint(input(Enter the number of parts you want to split in: ))output_files((file_namestr(i))foriinrange(1,parts1))st0pint(num_lines_orig/parts)edpforiinrange(parts-1):filenext(output_files)withopen(file,w)asOF:for_inrange(st,ed):OF.writelines(input_file.readline())sted
edstpifnum_lines_orig-ed
filenext(output_files)withopen(file,w)asOF:for_inrange(st,ed):OF.writelines(input_file.readline())if__name____main__:main()
使用分析器选项运行时
$ time py36-m memory_profilerSplitting\ text\ files_GOOD_usingGenerators.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3Filename:Splittingtext files_GOOD_usingGenerators.pyLine# Mem usage Increment Line Contents447.988MiB0.000MiBprofile()5defmain():647.988MiB0.000MiBfile_nameinput(Enter the full path of file you want to split into smaller inputFiles: )747.988MiB0.000MiBinput_fileopen(file_name)847.988MiB0.000MiBnum_lines_origsum(1for_ininput_file)947.988MiB0.000MiBinput_file.seek(0)1047.988MiB0.000MiBpartsint(input(Enter the number of parts you want to split in: ))1148.703MiB0.715MiBoutput_files((file_namestr(i))foriinrange(1,parts1))1247.988MiB-0.715MiBst01347.988MiB0.000MiBpint(num_lines_orig/parts)1447.988MiB0.000MiBedp1548.703MiB0.715MiBforiinrange(parts-1):1648.703MiB0.000MiBfilenext(output_files)1748.703MiB0.000MiBwithopen(file,w)asOF:1848.703MiB0.000MiBfor_inrange(st,ed):1948.703MiB0.000MiBOF.writelines(input_file.readline())202148.703MiB0.000MiBsted2248.703MiB0.000MiBedstp2348.703MiB0.000MiBifnum_lines_orig-ed