How to use a char[] buffer in D
Andrew Chapman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 22 14:57:04 PDT 2016
Hi everyone, just wanting some help with optimisation if anyone
is kind enough :-)
I have a loop that iterates potentially millions of times, and
inside that loop I have code that appends some strings together,
e.g.:
string key = s1 ~ "_" ~ s2;
I discovered that due to the memory allocation required, this
slows the execution significantly.
s1 is always a two character string, e.g "AA", and s2 is always a
single character.
What I want to do is something like this:
Outside the loop:
char[4] buffer;
buffer[2] = '_';
Then inside the loop
buffer[0] = s1[0];
buffer[1] = s1[1];
buffer[3] = s2[0];
This works OK, however, I then need to use the buffer value to
check for an existing value in a hashmap / associative array.
Code such as:
if(buffer in myHash) {
}
throws an access violation. A string value works without error.
Is there a way for me to use a buffer AND use it in functions
expecting strings?
I can use idup() on the char[] to make a string, but again we're
allocating memory which I'd rather avoid.
Thanks sincerely in advance,
Cheers,
Andrew.
More information about the Digitalmars-d-learn
mailing list