mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
reference: StreamGetSzPtr
git-svn-id: svn://ultimatepp.org/upp/trunk@8301 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
93a4d463a1
commit
ba970ec6da
4 changed files with 77 additions and 1 deletions
61
reference/StreamGetSzPtr/StreamGetSzPtr.cpp
Normal file
61
reference/StreamGetSzPtr/StreamGetSzPtr.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
int CountLines(Stream& s)
|
||||
{
|
||||
int n = 0;
|
||||
int c;
|
||||
while((c = s.Get()) >= 0)
|
||||
n += c == '\n';
|
||||
return n;
|
||||
}
|
||||
|
||||
int CountLinesOptimized(Stream& s)
|
||||
{
|
||||
int n = 0;
|
||||
for(;;) {
|
||||
int sz;
|
||||
const byte *p = s.GetSzPtr(sz);
|
||||
if(sz) {
|
||||
const byte *e = p + sz;
|
||||
const byte *e4 = p + (sz & ~3);
|
||||
while(p < e4) {
|
||||
n += (p[0] == '\n' || p[1] == '\n' || p[2] == '\n' || p[3] == '\n');
|
||||
p += 4;
|
||||
}
|
||||
while(p < e)
|
||||
n += *p++ == '\n';
|
||||
}
|
||||
else {
|
||||
int c = s.Get();
|
||||
if(c < 0)
|
||||
return n;
|
||||
n += c == '\n';
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
StdLogSetup(LOG_FILE|LOG_COUT);
|
||||
|
||||
const Vector<String>& arg = CommandLine();
|
||||
if(arg.GetCount() == 0) {
|
||||
RLOG("Please provide testing file path as argument, preferably >500MB");
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
RTIMESTOP("Simple");
|
||||
FileIn in(arg[0]);
|
||||
RDUMP(CountLines(in));
|
||||
}
|
||||
|
||||
{
|
||||
RTIMESTOP("Optimized");
|
||||
FileIn in(arg[0]);
|
||||
RDUMP(CountLinesOptimized(in));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue