|
The reason that the string builder gets slow when you put a lot of data in it, is that every time that it's capacity has to be increased, another buffer is allocated that is twice the size of the current buffer, and all the data is copied to the new buffer.
Method 3: Just write it all directly to a stream.
File output is buffered on at least two levels, so there is no reason for you to add another level of buffering. A FileStream uses a buffer so that it doesn't have to call the system for every byte written, and the system uses a buffer before writing anything to disk, as data can only be written in chunks of a cluster at a time (usually between one and eight kb). Then there is a write cache that works more or less as another buffer before the data is finally written to disk.
---
single minded; short sighted; long gone;
|
|
|
|
|
As leppie already indicated, StringBuilder.Capacity is the issue.
If you don't take care, a StringBuilder will allocate some buffer, and when it comes
to exceed its capacity, it will allocate a new buffer and copy everything.
IIRC it doubles the size every time.
The statement "use StringBuilder to speed up string operations" often does not mention
"and allocate ample capacity".
Writing smaller amounts to file is slower in theory, but using a stream should take care of
that based on its own buffering. (Warning: writing bytes one at a time would be much
slower because of the overhead involved in calling all the methods!).
So I would not doubt it a second, and write one row at a time. It simply makes no sense
to spend a lot of memory to collect things that do not need to be collected at all.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Yes. But I don't see the point in using the StringBuilder at all, just write the darn data to the file.
|
|
|
|
|
I agree an SB does not bring anything here.
I tried to explain the difference between the two proposals though.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Yes, and as long as he's clearing the StringBuilder (that's already been extended) rather than instantiating a new one each too.
|
|
|
|
|
Luc Pattyn wrote: The statement "use StringBuilder to speed up string operations" often does not mention
"and allocate ample capacity".
Probably because the StringBuilder performs pretty well even if you don't set the capacity.
25 minutes may seem a lot, but it's not that bad considering the amount of data. Based on the amount of data and the time it took for the StringBulder, some approximate calculations tells me that doing the same using string concatenation would need around 11 years to run.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: 25 minutes may seem a lot
it is a lot: it is much more than one would expect, independent of the approach, given the
simple job of writing a table to a file.
Guffa wrote: string concatenation would need around 11 years to run
hope your PC and power company will run that long without failure...
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc Pattyn wrote: hope your PC and power company will run that long without failure...
Well, you could build some protection into the program. If it saves the string to a temporary file every now and then, it could continue from the last save point in case of a failure.
---
single minded; short sighted; long gone;
|
|
|
|
|
On the other hand, there may be better ways[^] to spend idle CPU cycles...
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
TextBox tr2 = new TextBox();
tr2.Location = new Point(400, 163);
this.Controls.Add(tr2);
TextBox tr1 = new TextBox();
tr1.Location = new Point(495, 163);
this.Controls.Add(tr1);
textBox1.Text = Convert.ToInt32(tr2.Text) * Convert.ToInt32(tr1.Text) ;
}
where is the error that??
|
|
|
|
|
The error comes clearly from the last line. You try to convert the text of newly created TextBoxes to a number. As you haven't assigned anything to them they will contain an empty string. What value would you expect them to have?!?
|
|
|
|
|
Where do you set tr1.Text and tr2.Text? Based on the code you provided, they will both be an empty string.
|
|
|
|
|
Hello,
I'm trying to make a C# program that, in part, sends and receives emails. I'm using Gmail as the account, and can send emails from this program that would show as if I'd sent it from my Gmail account because it sends a command to Gmail containing my name and password, the email itself, who it goes to, etc.
That's fine, but the other half of the transfer is what's got me stumped--how to get the emails off that same Gmail account and show them on the screen.
Does anybody have any experience with this that can guide me in the right direction?
Thanks for your time,
Michael Fritzius
|
|
|
|
|
You need to use POP3 or IMAP protocols. Check out here[^] and here[^]. You will also need to enable POP3/IMAP access in your gmail account (through the gmail interface under settings).
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Thanks! I'm glad to see that one of those links was visited already so I guess I was on the right track. Those links were a ton of help.
Thanks again!
Michael Fritzius
|
|
|
|
|
Hi mates...i try to find a way to creating 2 or more texbox with 1 button for example if the user click the button i wanna create 2 or more textbox help plssss.
|
|
|
|
|
Write the following code in the event handler for the button:
TextBox textBox1 = new TextBox();
textBox1.Location = new Point(10, 10);
TextBox textBox2 = new TextBox();
textBox1.Location = new Point(10, 20);
this.Controls.Add(textBox1);
this.Controls.Add(textBox2);
That's it. You can of course set the other properties for the textboxes as well.
Virtual1ty
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
tr.text=convert.tostring(convert.toint32 (textbox1) * convert.toint32 (textbox2))
where is the problem in there can u help me??,
|
|
|
|
|
and why its crate texboxes together i write different coordinates but they r together
|
|
|
|
|
You can't convert a textbox to a number. You want to use textbox1.Text. Also, use int.TryParse, so it doesn't blow up if it can't convert.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
private void createTextBox()
{
}
private void button1_Click(object sender, EventArgs e)
{
createTextBox();
createTextBox();
}
|
|
|
|
|
Maybe you should do this:
private void createTextBox(string name)
{
TextBox name = new TextBox();
}
private void button1_Click(object sender, EventArgs e)
{
createTextBox(box1);
createTextBox(box2);
} Otherwise they probably would cause an exception if two textboxes are created with the same names.
Virtual1ty
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
Hi!
I am trying to run a timer in my Form.
The form has a function that is being called from the timer handler but i have a problem.
this is the code:
private static void TimerHandler(Object myObject, EventArgs myEventArgs)
{
starttime.Stop();
starttime.Enabled = false;
Form1_Main();
}
if i use the static on the Hnaler i get this error:
Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.Form1_Main()' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 124 13 Update_Installer
if i do not use it i get this:
private void TimerHandler(Object myObject, EventArgs myEventArgs)
{
starttime.Stop();
starttime.Enabled = false;
Form1_Main();
}
Error:
Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.TimerHandler(object, System.EventArgs)' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 112 31 Update_Installer
The function is in the general Form.
What to do?
Thanks
|
|
|
|
|
ytubis wrote: Form1_Main();
That 2nd error you get - what line of code is the error occurring on? My bet is you have some static timer and you're telling it to send it's Tick event notification to a non-static method.
To solve this, just don't make the timer static, don't make TimerHandler static, and don't make Form1_Main static. Don't make anything static unless there's a specific reason to do so (e.g. if it doesn't need to use any instance members)
|
|
|
|
|
Hi,
I am guessing you are working on an app without a user interface, and are trying to run
everything from your static Main method and feel a need to have everything static then.
The easy solution to that is to immediately create an
object of a new class and make it run; then nothing needs to be static except for
the main method. Example:
static Main(...) {
Test test=new Test();
test.Run();
}
class Test {
Timer timer;
public Test() {
timer=new Timer();
timer.Tick+=new EventArgs(tickHandler);
}
public void Run() {
...
}
public void tickHandler(...) {
...
}
}
BTW: you can put your static Main method inside the Test class if you want to.
It does not need a separate file or class.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|