Re: Reading an Ascii string
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Mon, 10 Jul 2006 23:56:22 +0100
<"John" <->> wrote:
Thanks Larry, and everyone else for your very helpful comments. I'm
pleased that the group jumped on my comments. I've learnt quite a
bit.
...but I can't help thinking that the way that C# handles strings is
inefficient.
Göran Andersson commented: "to allocate and release objects is faster
in a garbage collected environment than in a traditional heap that
uses reference counters."
OK, but in C, I would only allocate the string once, and it would be
a fixed-length string of 41 characters (max num characters is 40 plus
one extra character for the null terminator). I wouldn't keep
allocating it and de-allocating it - I would use it repeatedly, and
if I'm using it a million times in a for loop, then surely this would
be much much faster than what C# does, since it doesn't need to be
allocated and de-allocated each time. Or am I missing something here?
Yes - what's involved in allocating 40 characters. On the managed heap,
that involves increasing a pointer by 80 bytes, checking whether or not
it's exceeded the boundaries of the generation, and (assuming it
hasn't) zeroing out the memory. It's not a lot of work.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Reading an Ascii string
- From: Göran Andersson
- Re: Reading an Ascii string
- References:
- Re: Reading an Ascii string
- From: Larry Lard
- Re: Reading an Ascii string
- Prev by Date: Re: Tough question on database NULLs.
- Next by Date: Re: TStringGrid equivalent?
- Previous by thread: Re: Reading an Ascii string
- Next by thread: Re: Reading an Ascii string
- Index(es):
Relevant Pages
|