Replacing tango.text.Ascii.isearch

torhu torhu at yahoo.com
Thu Oct 6 23:39:34 UTC 2022


On Thursday, 6 October 2022 at 21:36:48 UTC, rassoc wrote:

> And what kind of testing was that? Mind to share? Because I did 
> the following real quick and wasn't able to measure a "two 
> orders of magnitude" difference. Sure, the regex version came 
> on top, but they were both faster than the ruby baseline I 
> cooked up.

Originally I just loaded a one megabyte file and searched the 
whole thing. I changed it to split it into (40 000) lines 
instead, regex is about ten times faster then. I compile with 
-release -O -inline. Here is the second version:

```d
import std;
import std.datetime.stopwatch;

enum FILE = "test.lst";
string text;
string needle;

void test(bool delegate(string haystack) dg)
{

     auto sw = StopWatch(AutoStart.yes);
     int counter = 0;
     foreach (line; lineSplitter(text)) {
         if (dg(line))
             counter++;
     }
     sw.stop();
     writefln("%s", sw.peek());
     writefln("counter: %s", counter);
}

void main(char[][] args)
{
     enforce(args.length > 1, "Need a needle argument.");

     text = cast(string)read(FILE);
     needle = args[1].idup;
     auto re = regex(to!string(escaper(needle)), "i");
     string needleLower = needle.toLower();

     test((h) => !!h.matchFirst(re));
     test((h) => h.asLowerCase().canFind(needleLower));
}
```



More information about the Digitalmars-d-learn mailing list