On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote:

It seems faster than algorithms in Phobos. We would love to see this in our new Phobos.

```d
  void mallocReplace()

  void normalReplace()
    string result = str.replace(';',"");

}/* Console Out:
Replace: 436 msecs
Malloc : 259 msecs
*/
```

You're comparing apples and oranges. When benchmarking, at least look at the generated assembly first.

replace is not in Phobos, it's a D runtime vestige. It's not getting inlined even in release builds with lto, whereas that manual version would. Also, benchmark with runtime strings, not literals, otherwise the compiler might even swallow the thing whole.

What you're benchmarking is, basically, inlined optimized search in a literal versus a function call.

Reply via email to