Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created July 31, 2014 06:05
Show Gist options
  • Save lnicola/15987b2e3af8b543bce5 to your computer and use it in GitHub Desktop.
Save lnicola/15987b2e3af8b543bce5 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class stopwatch
{
std::chrono::high_resolution_clock::time_point start;
public:
stopwatch()
: start(std::chrono::high_resolution_clock::now())
{
}
~stopwatch()
{
auto duration = std::chrono::high_resolution_clock::now() - start;
cerr << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << " ms" << endl;
}
};
class ss
{
const char *p_;
public:
ss(const char *p)
: p_(p)
{
}
int get()
{
return *p_++;
}
void unget()
{
p_--;
}
};
template<typename Pred, typename Stream>
static string read_while(Pred pred, Stream &is)
{
char ch;
string s;
while ((ch = is.get()) && pred(ch))
s += ch;
is.unget();
return s;
}
int main()
{
istringstream is("hello world!");
for (int k = 10; --k > 0; )
{
stopwatch sw;
for (int i = 5000000; --i > 0; )
{
//is.seekg(is.beg);
ss is2("hello world!");
read_while(isalpha, is2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment