-
Notifications
You must be signed in to change notification settings - Fork 16
/
split_subparser.cpp
55 lines (43 loc) · 1.45 KB
/
split_subparser.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
47
48
49
50
51
52
53
54
55
// Contributed by Scott Yeager ([email protected])
// Requires subparser and subbuffer (https://github.com/VerizonDigital/json_parser)
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <vector>
#include <iterator>
#include "subbuffer.h"
#include "subparser.h"
int main()
{
std::string input_line;
std::vector<subbuffer> spline;
long count = 0;
timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
std::cin.sync_with_stdio(false); //disable synchronous IO
std::size_t numWords = 0;
std::size_t numChars = 0;
spline.reserve(100);
while(std::getline(std::cin, input_line))
{
spline.clear();
subparser flds(input_line, ' ', subparser::SKIP_EMPTY);
numWords += flds.split(spline);
for (std::vector<subbuffer>::const_iterator iter = spline.begin(); iter != spline.end(); ++iter)
numChars += (*iter).length();
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;
}