Memory allocation in D (noob question)
Janice Caron
caron800 at googlemail.com
Wed Dec 5 10:10:58 PST 2007
On 12/5/07, Sean Kelly <sean at f4.ca> wrote:
> Or the runtime could be changed to always copy. However, it would
> absolutely murder application performance for something like this:
>
> char[] buf;
> for( int i = 0; i < 1_000_000; ++i )
> buf ~= 'a';
It would, /unless/ we had a vector type (a C++ std::vector, not a math
vector). Java has something similar, I believe - immutable strings,
but also a StringBuffer type. (I could be wrong about the details).
Anyway, the point is, you'd just rewrite the above loop as:
Vector!(char) buf;
for( int i = 0; i < 1_000_000; ++i )
buf ~= 'a';
//
// and when you're done
//
return buf.toArray();
That sort of thing.
More information about the Digitalmars-d
mailing list