Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary Q
#1
I was told a string is 10 bytes + string length. Why then, is a string of 20 chars only 20 bytes???

EDIT: does anyone have AIM or MSN I could bug? I don think these questions are worth a whole post >.>

EDIT2: I think the whole string = string length + 10 is bullshit. I dont run into any problems if I only presume the string = string length.
Reply
#2
seraphelic Wrote:I was told a string is 10 bytes + string length. Why then, is a string of 20 chars only 20 bytes???

Because an array of chars (not a string of chars, theres no such thing) is not the same thing as a string. A string has a header and a 2-character terminator along with probably some other information to define it. Although you could easily write your own class to support char arrays for strings if the memory was so critical, it'd probably be slower in the end.

....

Test time! I'll edit with the results here in a couple minutes.

Edit:

Code:
class Program
    {
        static void Main(string[] args)
        {
            CharString cs = new CharString(); cs += "Hello";
            string s = "Hello";
            Stopwatch w = new Stopwatch();
            string s2;

            w.Start();
            for (int i = 0; i < 15000; i++)
                cs += i.ToString();
            w.Stop();
            Console.WriteLine("Write CharString: {0}", w.ElapsedMilliseconds);

            w.Reset();
            w.Start();
            for (int i = 0; i < 15000; i++)
                s += i.ToString();
            w.Stop();
            Console.WriteLine("Write String: {0}", w.ElapsedMilliseconds);

            w.Reset();
            w.Start();
            for (int i = 0; i < 15000; i++)
                s2 = cs.ToString();
            w.Stop();
            Console.WriteLine("Read CharString: {0}", w.ElapsedMilliseconds);

            w.Reset();
            w.Start();
            for (int i = 0; i < 15000; i++)
                s2 = s.ToString();
            w.Stop();
            Console.WriteLine("Read String: {0}", w.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }

    class CharString
    {
        char[] _buffer = new char[8];
        int _bufferPos = 0;
        string _sCache = null;

        public void Append(string s)
        {
            if (s == null || s.Length == 0)
                return;

            char[] chrs = s.ToCharArray();
            lock (this)
            {
                if (_bufferPos + chrs.Length >= _buffer.Length)
                    Array.Resize(ref _buffer, Math.Max(_bufferPos + chrs.Length, _buffer.Length * 2));
                Array.Copy(chrs, 0, _buffer, _bufferPos, chrs.Length);
                _bufferPos += chrs.Length;
                _sCache = null;
            }
        }

        public override string ToString()
        {
            lock (this)
            {
                if (_sCache == null)
                    _sCache = new string(_buffer, 0, _bufferPos);
            }
            return _sCache;
        }

        public static CharString operator +(CharString cs, string s)
        {
            cs.Append(s);
            return cs;
        }
    }

Simple CharString class that supports string appending and reading. Results:

Code:
Write CharString: 7
Write String: 430
Read CharString: 1
Read String: 0

The writing was a bit unexpected, but the reading is about how you'd expect it. But keep in mind that, even though this is C#, the implementation is probably going to be close language-by-language. The overhead of the write is probably a combination of the string being reconstructed every append instead of cached like I did (since heavy string writes often should use a class like .NET's StringBuilder) and overhead from validation checks. I added in some threading safety to be a little more fair. Wink

Point being although you can write a faster string class for a specific case, if you need more speed or memory, you shouldn't be using strings or are approaching things the wrong way. Smile
Reply
#3
Look, while I am intelligent enough to comprehend what you just said, there's no point in trying to when my question was so simple. Maybe I'll ask the audience one more time. Let me see if I have this right:

Dim first As String
Dim second As String * 10
first = "Lorem Ipsum" '11 chars

SO first is 32 bytes (10 + 2(11))? and second is 10 bytes? Lemme know if I know my stuff yet.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)