optimatization suggestion
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Sat Apr 1 21:40:49 PDT 2017
On Sat, Apr 01, 2017 at 05:15:54PM +0000, Inquie via Digitalmars-d wrote:
> I use a lot of code that has string appending and it seems like it could be
> optimized;
>
> wchar[] x;
>
> x ~= "This is one line";
> x ~= "This is another line";
You could have just written instead:
wchar[] x;
x ~= "This is one line" ~
"This is another line";
?
[...]
> More complex cases could be handled such as
>
> x ~= "This is one"~x~" line";
> x ~= "This is another line";
>
> which turns in to
>
> x ~= "This is one"~x~" lineThis is another line";
>
> Since these are done in ctfe, it may improve the speed significantly
> in some cases!? (usually the splitting up is for readability and to
> allow for easy modification)
If you want something done in CTFE, do this:
string makeLongString()
{
wchar[] x;
x ~= "blahblah";
x ~= "blehbleh";
x ~= "abc " ~ x ~ " def";
return x;
}
void main() {
enum e = makeLongString(); // enum forces CTFE
string x = e; // voila, everything done at compile-time
}
T
--
"Real programmers can write assembly code in any language. :-)" -- Larry Wall
More information about the Digitalmars-d
mailing list