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

July 2nd, 2009, 03:32 PM
|
|
Registered User
|
|
Join Date: Nov 2008
Location: , , .
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|

July 2nd, 2009, 07:16 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 859
Thanks: 12
Thanked 150 Times in 149 Posts
|
|
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?
|
|
The Following User Says Thank You to Lee Dumond For This Useful Post:
|
|

July 4th, 2009, 11:59 AM
|
|
Registered User
|
|
Join Date: Nov 2008
Location: , , .
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
thanks
Thank You for Your reply, this way it's clear now! Thank You!
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
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 |
|
 |