This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 hours ago.
Improve this questionI have this code to read lines and words from a large file.
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <boost/regex.hpp>
using namespace std;
bool boostreglike(const string & str, const string & regstr)
{
boost::regex regexp(regstr);
boost::smatch matches;
try{
return boost::regex_search(str, matches, regexp);
}catch (exception& e) {
return false;
}
return false;
}
long int curtime()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
main (int argc, char *argv[])
{
long int thistime = curtime();
ifstream ifile(argv[2]);
string strline, str(argv[1]);
while (std::getline(ifile, strline)){
if (argc>3){
std::vector<std::string> tokens;
std::string token;
std::stringstream ss(strline);
while (getline(ss, token, argv[3][0]))
tokens.push_back(token);
}
}
printf("Time eclapsed %u\n", curtime()-thistime);
}
If I compile it with c++11 or earlier version, it took 6.6s to read a 800M size file.
$ g++ -g test2.cpp -lboost_regex -o test2
$ tim开发者_如何学Goe ./test2 123456789 largefile.txt " "
Time eclapsed 6610
real 0m6.613s
user 0m6.507s
sys 0m0.106s
$ g++ -g -std=c++11 test2.cpp -lboost_regex -o test2_11
$ time ./test2_11 123456789 largefile.txt " "
Time eclapsed 6648
real 0m6.651s
user 0m6.528s
sys 0m0.122s
But if it's compiled with c++17 or c++20, it took 10s.
$ g++ -g -std=c++17 test2.cpp -lboost_regex -o test2_17
$ time ./test2_17 123456789 largefile.txt " "
Time eclapsed 10514
real 0m10.518s
user 0m10.403s
sys 0m0.113s
$ g++ -g -std=c++2a test2.cpp -lboost_regex -o test2_2a
$ time ./test2_2a 123456789 largefile.txt " "
Time eclapsed 10421
real 0m10.424s
user 0m10.313s
sys 0m0.111s
My g++ version is 8.3.1 20190311
I expected to gain better performance in c++17 or c++20, but the actual situation is quite the opposite.
Is it because the implementation ofstring
or vector
becomes more complicated or something else?.
精彩评论