python打开文本太慢怎么办_为什么我的C++文本文件解析脚本比Python脚本慢得多? - python...

我目前正在尝试自学c++,并且正在研究文件IO。我已经阅读了cplusplus.com教程,并且正在使用从那里学到的基本文件IO技术:

std::ifstream \\using this to open a read-only file

std::ofstream \\using this to create an output file

std::getline \\using this to read each line of the file

outputfile << linecontents \\using this to write to the output file

我有一个大约10MB的文本文件,其中包含前一百万个素数,它们之间用空格隔开,每行8个素数。我的目标是编写一个程序,该程序将打开文件,通读内容,并编写每行一个质数的新文件。我正在使用正则表达式在每行末尾去除空格,并用单个换行符替换每个数字之间的空格。

基本算法很简单:使用正则表达式,我修剪每行末尾的空格,并用换行符替换中间的空格,并将该字符串写入输出文件。我已经用c++和Python编写了“相同”算法(除了我使用内置的strip()函数删除开头和结尾的空格),而且Python程序要快得多!我期望相反。我认为(写得很好)的c++程序应该快如闪电,而Python程序要慢10到20倍。尽管在Python中进行了任何优化都是在后台进行的,但是它比我的“等效” C++程序要快得多。

我的正则表达式搜索:

std::tr1::regex rxLeadingTrailingWS("^(\\s)+|(\\s)+$"); //whitespace at beginning or end of string

std::tr1::regex rxWS("(\\s)+"); //whitespace anywhere

我的文件解析代码:

void ReWritePrimesFile()

{

std::ifstream readFile("..//primes1.txt");

std::ofstream reducedPrimeList("..//newprimelist.txt");

std::string readout;

std::string tempLine;

std::tr1::regex rxLeadingTrailingWS("^(\\s)+|(\\s)+$"); //whitespace at beginning or end of string

std::tr1::regex rxWS("(\\s)+"); //whitespace anywhere

std::tr1::cmatch res; //the variable which a regex_search writes its results to

while (std::getline(readFile, readout)){

tempLine = std::tr1::regex_replace(readout.c_str(), rxLeadingTrailingWS, ""); //remove leading and trailing whitespace

reducedPrimeList << std::tr1::regex_replace(tempLine.c_str(), rxWS, "\n") << "\n"; //replace all other whitespace with newlines

}

reducedPrimeList.close();

}

但是,此代码需要几分钟才能解析10 MB的文件。以下Python脚本大约需要1-3秒(尚未计时):

import re

rxWS = r'\s+'

with open('pythonprimeoutput.txt', 'w') as newfile:

with open('primes1.txt', 'r') as f:

for line in f.readlines():

newfile.write(re.sub(rxWS, "\n", line.strip()) + "\n")

唯一显着的区别是,我使用内置的strip()函数剥离换行符,而不是使用正则表达式。 (这是我执行时间非常慢的原因吗?)

我完全不确定我的程序效率低下的原因是什么。一个10MB的文件应该花很长时间才能解析!

*编辑:最初显示的文件为20MB,只有10MB。

根据Nathan Oliver的建议,我使用了以下代码,仍然需要大约5分钟才能运行。现在,这几乎与我在Python中使用的算法相同。仍然不确定有什么不同。

void ReWritePrimesFile()

{

std::ifstream readFile("..//primes.txt");

std::ofstream reducedPrimeList("..//newprimelist.txt");

std::string readout;

std::string tempLine;

//std::tr1::regex rxLeadingTrailingWS("^(\\s)+|(\\s)+$"); //whitespace at beginning or end of string

std::tr1::regex rxWS("(\\s)+"); //whitespace anywhere

std::tr1::cmatch res; //the variable which a regex_search writes its results to

while (readFile >> readout){

reducedPrimeList << std::tr1::regex_replace(readout.c_str(), rxWS, "\n") + "\n"; //replace all whitespace with newlines

}

reducedPrimeList.close();

}

第二次编辑:我必须在regex_replace行的末尾添加一个额外的换行符。显然readFile >>读出在每个空格字符处停止了吗?不知道它是如何工作的,但是它为文件中的每个数字(而不是文件中的每一行)运行while循环的迭代。

python大神给出的解决方案

您拥有的代码较慢,因为您正在C++代码中执行两个正则表达式调用。就是这样,您知道是否使用>>运算符从文件中读取文件,它将忽略前导空格并读取直到找到另一个空格字符。您可以轻松地编写如下函数:

void ReWritePrimesFile()

{

std::ifstream readFile("..//primes1.txt");

std::ofstream reducedPrimeList("..//newprimelist.txt");

std::string readout;

while(readFile >> readout)

reducedPrimeList << readout << '\n';

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值