how to be faster than perl?
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Tue Jan 30 05:25:23 PST 2007
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?
I don't think the file handling is your problem.
> ---snip---
> private import std.stream;
> private import std.stdio;
> private import std.string;
>
> void main (char[][] args) {
> int c;
> Stream file = new BufferedFile(args[1]);
> foreach(ulong n, char[] line; file) {
> if(std.regexp.find(line, "horizontal") > -1){
> c++;
> }
> }
>
> writefln("%d", c);
>
> }
std.regexp.find instantiates a RegExp object, compiles the regex and
uses it once, then deletes it. This is fine for one-time searches, but
if you're using it for each line of a file, you're allocating and
deleting an object for each line and performing unnecessary work to
recompile the same regex over and over.
Try something like this instead:
-----
// (untested code)
auto re = new RegExp("horizontal");
foreach (ulong n, char[] line; file) {
if (re.find(line) > -1) {
// ...
-----
as the start of your foreach loop.
That should be faster.
I don't know how fast it'll be compared to Perl; I don't know anything
about the relative performance of D vs. Perl regexes. (In fact, I hardly
ever use regexes, and have never used Perl)
More information about the Digitalmars-d-learn
mailing list