-
Notifications
You must be signed in to change notification settings - Fork 16
/
splitc1.cpp
61 lines (50 loc) · 1.71 KB
/
splitc1.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
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <ctime>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
void splitc1(vector<string> &tokens, const string &str,
const string &delimiters = " ") {
char *saveptr;
char *cpy, *token;
cpy = (char*)malloc(str.size() + 1);
strcpy(cpy, str.c_str());
for(token = strtok_r(cpy, delimiters.c_str(), &saveptr);
token != NULL;
token = strtok_r(NULL, delimiters.c_str(), &saveptr)) {
tokens.push_back(string(token));
}
free(cpy);
}
int main() {
string input_line;
vector<string> spline;
long count = 0;
timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
cin.sync_with_stdio(false); //disable synchronous IO
size_t numWords = 0;
size_t numChars = 0;
while(getline(cin, input_line)) {
spline.clear(); //empty the vector for the next line to parse
splitc1(spline, input_line);
numWords += spline.size();
for (vector<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;
cerr << "C++ : Saw " << count << " lines (" << numWords << " words/" << numChars << " chars) in " << fixed << setprecision(1) << sec << " seconds." ;
if (sec > 0) {
const double lps = count / sec;
cerr << " Crunch speed: " << fixed << setprecision(1) << lps << endl;
} else
cerr << endl;
return 0;
}