我正在尝试将一些代码从Python转换为C++,以便获得一点速度并提高我生锈的C++技能.昨天我感到震惊的是,在Python中,从stdin读取行的简单实现要比C++快得多(参见本文).今天,我终于想出了如何在C++中使用合并分隔符(与python的split()类似的语义)拆分字符串,现在我正在体验似曾相识!我的C++代码需要更长的时间才能完成工作(尽管不是一个数量级,就像昨天的课程一样).
Python代码:
#!/usr/bin/env python
from __future__ import print_function
import time
import sys
count = 0
start_time = time.time()
dummy = None
for line in sys.stdin:
dummy = line.split()
count += 1
delta_sec = int(time.time() - start_time)
print("Python: Saw {0} lines in {1} seconds. ".format(count, delta_sec), end='')
if delta_sec > 0:
lps = int(count/delta_sec)
print(" Crunch Speed: {0}".format(lps))
else:
print('')
Run Code Online (Sandbox Code Playgroud)
C++代码:
#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <vector>
using namespace std;
void split1(vector<string> &tokens, const string …Run Code Online (Sandbox Code Playgroud) 2013年主题演讲:Chandler Carruth:优化C++的紧急结构
任何人都可以把这个放在这个答案的上下文中:https://stackoverflow.com/a/14229152
我听说不断重复,但是,对我而言,返回某些东西的功能是一个来源.通过引用的输出参数从功能中获取该特性,并且从功能中移除这种硬编码特性允许人们在外部管理,输出将如何被存储/重用.
我的问题是,即使在SO答案的上下文中,也有一种方法可以告诉,重构代码还有一些其他等效方式,"好了,现在看看,这种方式的值语义不会因输出参数版本而丢失",或者钱德勒的评论是针对一些人为的情况而定的?我甚至看过Andrei Alexandrescu在一次演讲中争论这个并且告诉你无法通过ref输出来逃避使用以获得更好的性能.
关于Andrei评论的另一个观点,请参阅Eric Niebler:Out Parameters,Move Semantics和Stateful Algorithms.
可能是python的C正则表达式实现快了6倍还是我错过了什么?
Python版本:
import re
r=re.compile(r'(HELLO).+?(\d+)', re.I)
s=r"prefixdfadfadf adf adf adf adf he asdf dHello Regex 123"
%timeit r.search(s)
1000000 loops, best of 3: 1.3 µs per loop (769,000 per sec)
Run Code Online (Sandbox Code Playgroud)
C++ 11版本:
#include<regex>
int main(int argc, char * argv[])
{
std::string s = "prefixdfadfadf adf adf adf adf he asdf dHello Regex 123";
std::regex my(R"((HELLO).+?(\d+))", regex_constants::icase);
bench_utils::run(std::chrono::seconds(10),
[&]{
std::smatch match;
bool found = std::regex_search(s, match, my);
});
return 0;
}
Results in about ~125,000 searches/second
Run Code Online (Sandbox Code Playgroud)
编辑: 这是bench_utils的代码:
namespace bench_utils
{ …Run Code Online (Sandbox Code Playgroud) 我比较了Linux C正则表达式库,
#include <iostream>
#include <chrono>
#include <regex.h>
int main()
{
const int count = 100000;
regex_t exp;
int rv = regcomp(&exp, R"_(([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?)_", REG_EXTENDED);
if (rv != 0) {
std::cout << "regcomp failed with " << rv << std::endl;
}
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < count; i++)
{
regmatch_t match;
const char *sz = "http://www.abc.com";
if (regexec(&exp, sz, 1, &match, 0) == 0) {
// std::cout << sz << " matches characters …Run Code Online (Sandbox Code Playgroud) 我正在VS中运行一个c ++程序.我提供了一个正则表达式,我正在解析一个超过200万行的文件,用于匹配该正则表达式的字符串.这是代码:
int main() {
ifstream myfile("file.log");
if (myfile.is_open())
{
int order_count = 0;
regex pat(R"(.*(SOME)(\s)*(TEXT).*)");
for (string line; getline(myfile, line);)
{
smatch matches;
if (regex_search(line, matches, pat)) {
order_count++;
}
}
myfile.close();
cout << order_count;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该文件应搜索匹配的字符串并计算它们的出现次数.我有一个python版本的程序,使用相同的正则表达式在4秒内完成.我已经等了大约5分钟才能使上面的c ++代码工作,但还没有完成.它没有遇到无限循环,因为我让它以一定的间隔打印出当前行号并且它正在进行中.我应该用不同的方式编写上面的代码吗?
编辑:这是在发布模式下运行.
编辑:这是python代码:
class PythonLogParser:
def __init__(self, filename):
self.filename = filename
def open_file(self):
f = open(self.filename)
return f
def count_stuff(self):
f = self.open_file()
order_pattern = re.compile(r'(.*(SOME)(\s)*(TEXT).*)')
order_count = 0
for line in f:
if order_pattern.match(line) != None:
order_count+=1 # …Run Code Online (Sandbox Code Playgroud) 我正在学习c ++和java中的正则表达式.所以我对c ++ 11正则表达式和java正则表达式进行了性能测试,表达式相同且输入相同.奇怪的是,java正则表达式比c ++ 11正则表达式更快.我的代码有什么问题吗?请纠正我
Java代码:
import java.util.regex.*;
public class Main {
private final static int MAX = 1_000_000;
public static void main(String[] args) {
long start = System.currentTimeMillis();
Pattern p = Pattern.compile("^[\\w._]+@\\w+\\.[a-zA-Z]+$");
for (int i = 0; i < MAX; i++) {
p.matcher("abcd_ed123.t12y@haha.com").matches();
}
long end = System.currentTimeMillis();
System.out.print(end-start);
}
}
Run Code Online (Sandbox Code Playgroud)
C++代码:
#include <iostream>
#include <Windows.h>
#include <regex>
using namespace std;
int main()
{
long long start = GetTickCount64();
regex pat("^[\\w._]+@\\w+\\.[a-zA-Z]+$");
for (long i = 0; i < …Run Code Online (Sandbox Code Playgroud) c++ ×5
regex ×4
c++11 ×3
python ×3
performance ×2
benchmarking ×1
c ×1
java ×1
optimization ×1
split ×1
string ×1