p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

Go Back   p2p.wrox.com Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
I forgot my password
Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 2nd, 2009, 03:32 PM
vik vik is offline
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2008
Location: , , .
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 2nd, 2009, 07:16 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,544, Level: 28
Points: 4,544, Level: 28 Points: 4,544, Level: 28 Points: 4,544, Level: 28
Activity: 48%
Activity: 48% Activity: 48% Activity: 48%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 859
Thanks: 12
Thanked 150 Times in 149 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?
__________________
Author of the upcoming ASP.NET 4.0 Website Programming: Problem - Design - Solution

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

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to Lee Dumond For This Useful Post:
vik (July 4th, 2009)
  #3 (permalink)  
Old July 4th, 2009, 11:59 AM
vik vik is offline
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2008
Location: , , .
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Storage Engine ashuphp Beginning PHP 1 April 19th, 2007 01: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 02:31 PM
date storage simplyAns Oracle 4 October 20th, 2004 04:59 AM
Document Storage hugh@kmcnetwork.com SQL Server 2000 1 December 23rd, 2003 10:36 AM



All times are GMT -4. The time now is 11:44 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2010 Wiley Publishing, Inc