Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 2nd, 2009, 02:32 PM
vik vik is offline
Registered User
 
Join Date: Nov 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default storage space of strings

Greetings to everybody!

As far as I know a string needs as much memory as many character it contains.
Opposing this theory I have two - theoretically equivalent - ways of building a string, but the storage space seems to be very different. Here is the code:

Code:
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();

            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            bf.Serialize(fs, a);
            fs.Close();
        }
    }

    [Serializable]
    class A
    {
        List<string> aha;
        public List<string> Aha
        {
            get { return aha; }
        }
        public A()
        {
            aha = new List<string>();
            for (int i = 0; i < 10000; i++)
            {
                aha.Add("a9999b");
            }
        }
    }
and the other version:

Code:
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();

            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            bf.Serialize(fs, a);
            fs.Close();
        }
    }

    [Serializable]
    class A
    {
        List<string> aha;
        public List<string> Aha
        {
            get { return aha; }
        }
        public A()
        {
            aha = new List<string>();
            for (int i = 0; i < 10000; i++)
            {
                aha.Add("a" + i.ToString() + "b");
            }
        }
    }
The only difference is the way the string is built, and all the strings in the first version are non-longer than the ones in the second, so I thought that file will occupy less bytes. But surprisingly the first file's size is 50kB and the second's is 117kB.
Could You tell me what's wrong? Are my starting hypothesys' wrong or is this normal, and if not how should the second solution occupy the same amount of memory (or less, as it's strings are shorter?)?
Thank You in advance!
Vik
 
Old July 2nd, 2009, 06:16 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

This is because of the way the BinaryFormatter's Serialize method works.

When a series of identical bytes is serialized using the BinaryFormatter, it doesn't just repeat the whole sequence over and over. It optimizes by using the format to indicate that the same sequence of bytes is repeated.

Since the first stream consists of "a9999b" over and over, a lot of space is saved there.

In the second stream, each sequence of bytes is unique; therefore, the formatter cannot optimize in the same fashion, and so the stream ends up being much larger.

Make sense?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
vik (July 4th, 2009)
 
Old July 4th, 2009, 10:59 AM
vik vik is offline
Registered User
 
Join Date: Nov 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default thanks

Thank You for Your reply, this way it's clear now! Thank You!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Storage Engine ashuphp Beginning PHP 1 April 19th, 2007 12:13 AM
Increase mysql storage mgnishan MySQL 0 February 7th, 2007 01:37 AM
intermediate storage sarah lee ASP.NET 1.0 and 1.1 Basics 3 September 11th, 2006 01:31 PM
date storage simplyAns Oracle 4 October 20th, 2004 03:59 AM
Document Storage [email protected] SQL Server 2000 1 December 23rd, 2003 10:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.