Re: Reading an Ascii string

Tech-Archive recommends: Speed Up your PC by fixing your registry



<"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
.



Relevant Pages

  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: iso_varying_string in f2003?
    ... > You're proposing that the compiler reallocate (if ... > Only if there is such a length (i.e. if the string is allocated). ... remaining characters of the present input record. ... then the result is to allocate the string to the appropriate length ...
    (comp.lang.fortran)
  • Re: How to take in a string of any size?
    ... #> You could write it to a file, counting the characters as you go, ... #> and then allocate the memory and read it back in. ... which leads to the original question of how do you read a string ...
    (comp.lang.c)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)