-
Notifications
You must be signed in to change notification settings - Fork 16
/
split9.cpp
46 lines (37 loc) · 1.3 KB
/
split9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::string input_line;
long count = 0;
timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
std::cin.sync_with_stdio(false); //disable synchronous IO
std::vector<std::string> spline;
std::size_t numWords = 0;
std::size_t numChars = 0;
while(std::getline(std::cin, input_line)) {
boost::algorithm::split(spline, input_line, boost::algorithm::is_any_of(" "));
numWords += spline.size();
for (std::vector<std::string>::const_iterator iter = spline.begin(); iter != spline.end(); ++iter)
numChars += iter->size();
count++;
};
timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
const double sec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
std::cerr << "C++ : Saw " << count << " lines (" << numWords << " words/" << numChars << " chars) in " << std::fixed << std::setprecision(1) << sec << " seconds." ;
if (sec > 0)
{
const double lps = count / sec;
std::cerr << " Crunch speed: " << std::fixed << std::setprecision(1) << lps << std::endl;
}
else
std::cerr << std::endl;
return 0;
}