How to loop through characters of a string in D language?
Stanislav Blinov
stanislav.blinov at gmail.com
Thu Dec 23 16:13:49 UTC 2021
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.
More information about the Digitalmars-d-learn
mailing list