Re: Counting words in text file (Mirek Fidler -- : was Java - c++, IO)
On Sun, 30 Mar 2008 04:12:52 -0500, Razii
<DONTwhatevere3e@hotmail.com> wrote:
C:\>WCUPP bible2.txt
Time: 843 ms
Gosh! that's fast. Just for fun, I tried this:
C:\>WCUPP bible2.txt bible2.txt bible2.txt bible2.txt bible2.txt
bible2.txt
Time: 4937 ms
C:\>java -server WordCount2 bible2.txt bible2.txt bible2.txt
bible2.txt bible2.txt
Time: 14375 ms
U++ is almost 3 times faster!
At least when compared to VC++
C:\>wc1 bible2.txt bible2.txt bible2.txt bible2.txt bible2.txt
Time: 28515 ms
My version is twice faster :) I am waiting for C++ gurus to show me
C++ version that uses standard library and is faster than my version.
And I am on Windows and was using this. only thing I changed was added
internal timer
--------
#include <Core/Core.h>
using namespace Upp;
#include <ctime>
int main(int argc, const char *argv[])
{
int n;
VectorMap<String, int> map;
Cout() << " lines words bytes file\n";
//TIME STARTS HERE
clock_t start=clock();
int total_lines = 0;
int total_words = 0;
int total_bytes = 0;
for(int i = 1; i < argc; i++) {
String f = LoadFile(argv[i]);
int lines = 0;
int words = 0;
const char *q = f;
for(;;) {
int c = *q;
if(IsAlpha(c)) {
const char *b = q++;
while(IsAlNum(*q)) q++;
map.GetAdd(String(b, q), 0)++;
words++;
}
else {
if(!c) break;
if(c == '\n')
++lines;
q++;
}
}
Cout() << Format("%8d%8d%8d %s\n", lines, words, f.GetCount(),
argv[i]);
total_lines += lines;
total_words += words;
total_bytes += f.GetCount();
}
Vector<int> order = GetSortOrder(map.GetKeys());
//TIME ENDS HERE
clock_t end=clock();
Cout() << Format("--------------------------------------%8d%8d%8d
total\n", total_lines, total_words, total_bytes);
for(int i = 0; i < order.GetCount(); i++)
Cout() << map.GetKey(order[i]) << ": " << map[order[i]] <<
'\n';
Cout()<<"Time: " <<
double(end-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
return 0;
}