Connecting Tech Pros Worldwide Forums | Help | Site Map

Generating very large string in C#

Dukkov@yahoo.com
Guest
 
Posts: n/a
#1: Jun 13 '06
Hi Folks,

I need to generate a very large string (1 MB or so) in my C# code, so I
can test the code.

What is the most elegant way to do so?

Thanks!

Dim


Scott M.
Guest
 
Posts: n/a
#2: Jun 13 '06

re: Generating very large string in C#


Use the StringBuilder class and its Append method.


<Dukkov@yahoo.com> wrote in message
news:1150156749.646893.53700@h76g2000cwa.googlegro ups.com...[color=blue]
> Hi Folks,
>
> I need to generate a very large string (1 MB or so) in my C# code, so I
> can test the code.
>
> What is the most elegant way to do so?
>
> Thanks!
>
> Dim
>[/color]


tanyrad@gmail.com
Guest
 
Posts: n/a
#3: Jun 13 '06

re: Generating very large string in C#


Is this really elegant?

What if I use directly StringBuilder str = new
StringBuilder(UInt32.Max)? or smaller capacity?

Thanks!

Scott M. wrote:[color=blue]
> Use the StringBuilder class and its Append method.
>
>
> <Dukkov@yahoo.com> wrote in message
> news:1150156749.646893.53700@h76g2000cwa.googlegro ups.com...[color=green]
> > Hi Folks,
> >
> > I need to generate a very large string (1 MB or so) in my C# code, so I
> > can test the code.
> >
> > What is the most elegant way to do so?
> >
> > Thanks!
> >
> > Dim
> >[/color][/color]

Barry Kelly
Guest
 
Posts: n/a
#4: Jun 13 '06

re: Generating very large string in C#


tanyrad@gmail.com wrote:
[color=blue]
> Is this really elegant?
>
> What if I use directly StringBuilder str = new
> StringBuilder(UInt32.Max)? or smaller capacity?[/color]

This won't compile - for one, StringBuilder takes an Int32 argument, and
for another, it's UInt32.MaxValue. If you pass Int32.MaxValue, it should
work on a 64-bit system, although it might be slow if there isn't enough
memory.

The OP specified on the order of 1MB, so talk of such large cases as 4GB
(int.MaxValue * sizeof(char)) is fairly irrelevant.

If the task is generating a string in an output fashion only (i.e. no
random access, replacements, insertions etc. needed), where it has to
scale far beyond 1MB or so, writing to a file will be more efficient,
but then the string won't be in memory, and so the source code itself
that works with the string may be less elegant.

-- Barry

--
http://barrkel.blogspot.com/
Mattias Sjögren
Guest
 
Posts: n/a
#5: Jun 13 '06

re: Generating very large string in C#


>I need to generate a very large string (1 MB or so) in my C# code, so I[color=blue]
>can test the code.
>
>What is the most elegant way to do so?[/color]

string large = new string('?', 512*1024);

Should take roughly 1 MB.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Jun 13 '06

re: Generating very large string in C#


<Dukkov@yahoo.com> wrote:[color=blue]
> I need to generate a very large string (1 MB or so) in my C# code, so I
> can test the code.
>
> What is the most elegant way to do so?[/color]

string x = new string ('x', 1024*1024/2);

(The division by two is to take account of each character being 2
bytes.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Willy Denoyette [MVP]
Guest
 
Posts: n/a
#7: Jun 13 '06

re: Generating very large string in C#



"Barry Kelly" <barry.j.kelly@gmail.com> wrote in message
news:ce4s82l6fv8l1tji527nuf71h4k9av8llo@4ax.com...
| tanyrad@gmail.com wrote:
|
| > Is this really elegant?
| >
| > What if I use directly StringBuilder str = new
| > StringBuilder(UInt32.Max)? or smaller capacity?
|
| This won't compile - for one, StringBuilder takes an Int32 argument, and
| for another, it's UInt32.MaxValue. If you pass Int32.MaxValue, it should
| work on a 64-bit system, although it might be slow if there isn't enough
| memory.
|

It won't work on a 64 bit system either, a string (like any other CLR
object) is limitted to 2GB irrespective the OS version.

Willy.




Barry Kelly
Guest
 
Posts: n/a
#8: Jun 13 '06

re: Generating very large string in C#


"Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote:
[color=blue]
> It won't work on a 64 bit system either, a string (like any other CLR
> object) is limitted to 2GB irrespective the OS version.[/color]

I have not yet had the luxury or necessity to test on a 64-bit sysem
yet... :)

-- Barry

--
http://barrkel.blogspot.com/
Willy Denoyette [MVP]
Guest
 
Posts: n/a
#9: Jun 13 '06

re: Generating very large string in C#



"Barry Kelly" <barry.j.kelly@gmail.com> wrote in message
news:h6ts82dicgq8spm7qvk9e53q0i61a0cpmg@4ax.com...
| "Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote:
|
| > It won't work on a 64 bit system either, a string (like any other CLR
| > object) is limitted to 2GB irrespective the OS version.
|
| I have not yet had the luxury or necessity to test on a 64-bit sysem
| yet... :)
|

If yo can't test it yourself, just read this snip:
<First some background; in the 2.0 version of the .Net runtime (CLR) we made
a conscious design decision to keep the maximum object size allowed in the
GC Heap at 2GB, even on the 64-bit version of the runtime.>

from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx

Willy.


Barry Kelly
Guest
 
Posts: n/a
#10: Jun 13 '06

re: Generating very large string in C#


"Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote:
[color=blue]
> If yo can't test it yourself, just read this snip:[/color]

Yes. Now that you mentioned it, I do dimly recall this. However, I
usually test everything I post on the newsgroup here, so I would have
tested it anyway.

-- Barry

--
http://barrkel.blogspot.com/
Scott M.
Guest
 
Posts: n/a
#11: Jun 13 '06

re: Generating very large string in C#


Still not a good idea if the string is to be built over several steps,
rather than just one assignment. StringBuilder would be better.


"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eaDMWxqjGHA.4344@TK2MSFTNGP05.phx.gbl...[color=blue][color=green]
> >I need to generate a very large string (1 MB or so) in my C# code, so I
>>can test the code.
>>
>>What is the most elegant way to do so?[/color]
>
> string large = new string('?', 512*1024);
>
> Should take roughly 1 MB.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#12: Jun 13 '06

re: Generating very large string in C#


Scott M. <s-mar@nospam.nospam> wrote:[color=blue]
> Still not a good idea if the string is to be built over several steps,
> rather than just one assignment. StringBuilder would be better.[/color]

Why? I don't see anything in the OP's original post to indicate that
anything beyond a large string is required. Using that constructor is
the simplest way of generating such a string, IMO. In what way would
StringBuilder be better?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Göran Andersson
Guest
 
Posts: n/a
#13: Jun 14 '06

re: Generating very large string in C#


Willy Denoyette [MVP] wrote:[color=blue]
> "Barry Kelly" <barry.j.kelly@gmail.com> wrote in message
> news:h6ts82dicgq8spm7qvk9e53q0i61a0cpmg@4ax.com...
> | "Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote:
> |
> | > It won't work on a 64 bit system either, a string (like any other CLR
> | > object) is limitted to 2GB irrespective the OS version.
> |
> | I have not yet had the luxury or necessity to test on a 64-bit sysem
> | yet... :)
> |
>
> If yo can't test it yourself, just read this snip:
> <First some background; in the 2.0 version of the .Net runtime (CLR) we made
> a conscious design decision to keep the maximum object size allowed in the
> GC Heap at 2GB, even on the 64-bit version of the runtime.>
>
> from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx
>
> Willy.
>[/color]

Another "who would ever need more than 640 kb" decision.... ;)
Scott M.
Guest
 
Posts: n/a
#14: Jun 14 '06

re: Generating very large string in C#


As stated, a StringBuilder would be better *if* the string is to be built up
over several expressions.


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1ef93c28844b3f5c98d24b@msnews.microsoft.c om...[color=blue]
> Scott M. <s-mar@nospam.nospam> wrote:[color=green]
>> Still not a good idea if the string is to be built over several steps,
>> rather than just one assignment. StringBuilder would be better.[/color]
>
> Why? I don't see anything in the OP's original post to indicate that
> anything beyond a large string is required. Using that constructor is
> the simplest way of generating such a string, IMO. In what way would
> StringBuilder be better?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#15: Jun 14 '06

re: Generating very large string in C#


Scott M. <s-mar@nospam.nospam> wrote:[color=blue]
> As stated, a StringBuilder would be better *if* the string is to be built up
> over several expressions.[/color]

Better than a concatenation, yes. I don't think anyone was suggesting
that though.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Göran Andersson
Guest
 
Posts: n/a
#16: Jun 14 '06

re: Generating very large string in C#


Jon Skeet [C# MVP] wrote:[color=blue]
> Scott M. <s-mar@nospam.nospam> wrote:[color=green]
>> As stated, a StringBuilder would be better *if* the string is to be built up
>> over several expressions.[/color]
>
> Better than a concatenation, yes. I don't think anyone was suggesting
> that though.
>[/color]

No, the only suggested alternative was using a string constructor that
creates a huge string filled with the same character.

That fulfills the only requirements that the OP had, e.g. that the
string should be large, and that the way to create it should be elegant.

If that solution really is sufficient for the test that the OP is going
to do depends on information about the test that was not revealed in the
original post.

Perhaps time (e.g. the OP) will tell... ;)
Willy Denoyette [MVP]
Guest
 
Posts: n/a
#17: Jun 14 '06

re: Generating very large string in C#



"Göran Andersson" <guffa@guffa.com> wrote in message
news:OFY%23j%23zjGHA.4652@TK2MSFTNGP05.phx.gbl...
| Willy Denoyette [MVP] wrote:
| > "Barry Kelly" <barry.j.kelly@gmail.com> wrote in message
| > news:h6ts82dicgq8spm7qvk9e53q0i61a0cpmg@4ax.com...
| > | "Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote:
| > |
| > | > It won't work on a 64 bit system either, a string (like any other
CLR
| > | > object) is limitted to 2GB irrespective the OS version.
| > |
| > | I have not yet had the luxury or necessity to test on a 64-bit sysem
| > | yet... :)
| > |
| >
| > If yo can't test it yourself, just read this snip:
| > <First some background; in the 2.0 version of the .Net runtime (CLR) we
made
| > a conscious design decision to keep the maximum object size allowed in
the
| > GC Heap at 2GB, even on the 64-bit version of the runtime.>
| >
| > from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx
| >
| > Willy.
| >
|
| Another "who would ever need more than 640 kb" decision.... ;)

I don't see the link, anyway, remember reference type instances are
'moveable' unless they are larger than 85Kb, that means that strings larger
than 85Kb are stored on the large object heap which never gets compacted,
simply because it's too costly to move such large blocks of memory (ever
thought what it would take to move 2GB objects in memory).
Note also that .NET is not the silver bullet, if you really need such large
contigious strings, you'll have to allocate them from the process heap using
unmanaged code.

Willy.



Closed Thread