How to use a char[] buffer in D
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 22 15:41:24 PDT 2016
On Wed, Jun 22, 2016 at 09:57:04PM +0000, Andrew Chapman via Digitalmars-d-learn wrote:
> 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.
Yes, frequent allocation of small strings is a performance killer. Using
a static array (as you've done below) is much better.
> 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?
Maybe try:
if (buffer[] in myHash) { ... }
? Does that make a difference?
T
--
This sentence is false.
More information about the Digitalmars-d-learn
mailing list