Can C++;程序字符串搜索速度是否与python一样快? 我不确定为什么我在Python中编写程序的时间字符串搜索比我在C++中写的程序快。我错过了什么把戏吗
生成用例 这是针对单行用例的,但是在实际用例中,我关心多行Can C++;程序字符串搜索速度是否与python一样快? 我不确定为什么我在Python中编写程序的时间字符串搜索比我在C++中写的程序快。我错过了什么把戏吗,python,c++,regex,benchmarking,string-search,Python,C++,Regex,Benchmarking,String Search,生成用例 这是针对单行用例的,但是在实际用例中,我关心多行 #include "tchar.h" #include "stdio.h" #include "stdlib.h" #include <string> #include <sstream> #include <iostream> #include <fstream> #include <ctime> using namespace std; void main(void){
#include "tchar.h"
#include "stdio.h"
#include "stdlib.h"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
void main(void){
ofstream testfile;
unsigned int line_idx = 0;
testfile.open("testfile.txt");
for(line_idx = 0; line_idx < 50000u; line_idx++)
{
if(line_idx != 43268u )
{
testfile << line_idx << " dontcare" << std::endl;
}
else
{
testfile << line_idx << " care" << std::endl;
}
}
testfile.close();
}
Python程序需要0.02200秒
import sys, os # to navigate and open files
import re # to search file
import time # to benchmark
ptrn = re.compile(r'^(\d*)\s(care)$', re.MULTILINE)
start = time.time()
with open('testfile.txt','r') as testfile:
filetext = testfile.read()
matches = re.findall(ptrn, filetext)
print("Found? " + "Yes" if len(matches) == 1 else "No")
end = time.time()
print("Total time", end - start)
执行Ratah对8.923的建议 通过将文件读入单个字符串,大约5秒的改进
double duration;
std::clock_t start;
ifstream testfile("testfile.txt", ios_base::in);
unsigned int line_idx = 0;
bool found = false;
string line;
regex ptrn("^(\\d*)\\s(care)$");
std::smatch matches;
start = std::clock(); /* Debug time */
std::string test_str((std::istreambuf_iterator<char>(testfile)),
std::istreambuf_iterator<char>());
if(regex_search(test_str, matches, ptrn))
{
found = true;
}
testfile.close();
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout << "Found? " << (found ? "yes" : "no") << std::endl;
std::cout << " Total time: " << duration << std::endl;
双倍时长;
std::时钟未启动;
ifstream testfile(“testfile.txt”,ios_base::in);
无符号整数行_idx=0;
bool-found=false;
弦线;
regex ptrn(“^(\\d*)\\s(care)$”;
std::smatch匹配;
开始=标准::时钟();/*调试时间*/
std::string test_str((std::istreambuf_迭代器(testfile)),
std::istreambuf_迭代器();
if(正则表达式搜索(测试字符串、匹配项、ptrn))
{
发现=真;
}
testfile.close();
持续时间=(标准::时钟()-开始)/(双)每秒时钟;
STD::当您在C++循环中找到匹配时,尝试<代码>中断<代码>…也声明<代码> STD::sMatter匹配;<代码>在循环之外,以避免重复构造它。另一个区别是:在python中只执行一个对regex的调用,因为一次读取所有文件。用C++代码,你每行调用一次。编译时使用了哪些标志?不知怎么的,每个人都认为调试构建应该在C++中是很快的。
double duration;
std::clock_t start;
ifstream testfile("testfile.txt", ios_base::in);
unsigned int line_idx = 0;
bool found = false;
string line;
regex ptrn("^(\\d*)\\s(care)$");
std::smatch matches;
start = std::clock(); /* Debug time */
std::string test_str((std::istreambuf_iterator<char>(testfile)),
std::istreambuf_iterator<char>());
if(regex_search(test_str, matches, ptrn))
{
found = true;
}
testfile.close();
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout << "Found? " << (found ? "yes" : "no") << std::endl;
std::cout << " Total time: " << duration << std::endl;