how to be faster than perl?
Derek Parnell
derek at nomail.afraid.org
Wed Jan 31 00:25:39 PST 2007
On Tue, 30 Jan 2007 13:21:53 +0100, Boris Bukowski wrote:
> currently I am testing D for log processing.
> My perl script is more than ten times faster than my D Prog.
> How can I get Lines faster from a File?
Your example code seemed to be trying to count the number of times a
certain string occurred in a file so I didn't bother with working with
'lines' as such. Anyhow, here is one way to do it...
// findtext.d ---------
private import std.file;
private import std.stdio;
private import std.string;
void main (char[][] args) {
char[] lFileText; // Buffer for file contents.
int lCnt; // Number if hits
int lPos; // Found at position, or Not Found flag.
int lFrom; // Where in the file to look from.
// Grab the whole file into RAM
lFileText = cast(char[]) std.file.read(args[1]);
// Start scanning for the substring.
lFrom = 0;
while(lFrom < lFileText.length)
{
lPos = std.string.find(lFileText[lFrom..$], args[2]);
if (lPos != -1)
{
// Adjust next starting position.
lFrom += lPos + args[2].length;
// And count the hits, of course.
lCnt++;
}
else
{
// Force end of scanning.
lFrom = lFileText.length;
}
}
writefln("Count of '%s' found in '%s': %d",
args[2], args[1], lCnt);
}
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
31/01/2007 7:18:25 PM
More information about the Digitalmars-d-learn
mailing list