Skip to content

Add two implementations split10 and split11 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CXXFLAGS = -Wall -O3

all: split1 split2 split6 split7 split8 split9 splitc1 splitc2 splitc3 split_subparser
all: split1 split2 split6 split7 split8 split9 splitc1 splitc2 splitc3 split_subparser split10 split11 split12

split7: split7.cpp | deps/strtk
$(CXX) $(LDFLAGS) -Ideps/strtk/ $(CXXFLAGS) split7.cpp -o split7
Expand All @@ -12,7 +12,7 @@ split_subparser: split_subparser.cpp | deps/json_parser
.PHONY: clean update-deps

clean:
@rm -f split1 split2 split6 split7 split8 split9 splitc1 splitc2 splitc3 split_subparser
@rm -f split1 split2 split6 split7 split8 split9 splitc1 splitc2 splitc3 split_subparser split10 split11 split12

update-deps: deps
git --git-dir=deps/strtk/.git/ --work-tree=deps/strtk/ fetch origin
Expand Down
54 changes: 54 additions & 0 deletions split10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// http://stackoverflow.com/a/9379203/106471
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

void split(vector<string> &ret, const string& s, char delimiter=' ')
{
ret.clear();
for(auto itStart = s.begin(), itDelim = itStart, itEnd = s.end(); itStart < itEnd; itStart = itDelim + 1)
{
itDelim = find(itStart, itEnd, delimiter);
ret.emplace_back(string(itStart, itDelim));
}
}



int main() {
string input_line;
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;

vector<string> words;
while(getline(cin, input_line))
{
split(words, input_line);
numWords += words.size();
for(const auto &s:words)
numChars += s.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;
}
69 changes: 69 additions & 0 deletions split11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// http://stackoverflow.com/a/9379203/106471
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

class StringRef
{
private:
const char* m_itBeg;
int m_iSize;

public:
inline int size() const { return m_iSize; }
const char* begin() { return m_itBeg; }
const char* end() const { return m_itBeg + m_iSize; }

StringRef(const char* &beg, const char* &nd)
: m_itBeg(beg)
, m_iSize(nd - beg)
{}
};

int split(vector<StringRef> &ret, const string& s, const char delimiter = ' ')
{
for(auto ps = &s[0], pd = ps, pe = ps + s.size(); ps < pe; ps = pd + 1)
{
ret.emplace_back(StringRef(ps, pd = find(ps, pe, delimiter)));
}
return ret.size();
}

int main() {
string input_line;
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;

vector<StringRef> words;
words.reserve(100);
while(getline(cin, input_line))
{
words.clear();
numWords += split(words, input_line);
for(const auto &s:words)
numChars += s.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;
}
72 changes: 72 additions & 0 deletions split12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// http://stackoverflow.com/a/9379203/106471
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <sstream>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

class StringRef
{
private:
const char* m_itBeg;
int m_iSize;

public:
inline int size() const { return m_iSize; }
const char* begin() { return m_itBeg; }
const char* end() const { return m_itBeg + m_iSize; }

StringRef(const char* &beg, const char* &nd)
: m_itBeg(beg)
, m_iSize(nd - beg)
{}
};

int split(vector<StringRef> &ret, const string& s, const char delimiter = ' ')
{
auto ps = &s[0], pd = ps, pe = ps + s.size();
for(;pd; ps = pd + 1)
{
pd = (const char *)memchr(ps, delimiter, pe - ps);
ret.emplace_back(StringRef(ps, pd ? pd : pe));
}
return ret.size();
}

int main() {
string input_line;
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;

vector<StringRef> words;
words.reserve(100);
while(getline(cin, input_line))
{
words.clear();
numWords += split(words, input_line);
for(const auto &s:words)
numChars += s.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;
}