Suppose we have 3,800,000 textboxes. Each textbox contains some word.
we have a function GenerateIRSTaxCode(string s, string s2, ... string s3800000)
we have a button that will take these 3.8MM texboxes and pass them into the GenerateIRSTaxCode() function.
What will be the difference in performance by doing:
string s = textbox1.text;
string s1 = textbox2.text;
...
string s3800000= textbox3800000.text;
GenerateIRSTaxCode(s1, s2, ... s3800000)
VS
GenerateIRSTaxCode(textbox1.text, textbox2.text, ... textbox3800000.text)
EDIT:
So I've done some testing:
{
for (int j = 0; j < 5; j++)
{
DateTime starttime1 = DateTime.Now;
List<string> words = new List<string>();
for (int i = 0; i < 4000000; i++)
{
string s = textBox2.Text;
words.Add(s);
}
DateTime endtime1 = DateTime.Now;
DateTime starttime2 = DateTime.Now;
for (int i2 = 0; i2 < 4000000; i2++)
{
words.Add(textBox2.Text);
}
DateTime endtime2 = DateTime.Now;
TimeSpan elapsed1 = endtime1 - starttime1;
TimeSpan elapsed2 = endtime2 - starttime2;
TimeSpan performancehit = elapsed1 - elapsed2;
textBox1.AppendText("Assign Time start: " + starttime1 + "\r\nAssign Time End: " + endtime1 + "\r\n elapsed time: " + elapsed1.ToString() +
"\r\n NoAssign Time start: " + starttime2 + "\r\nNoAssign Time End: " + endtime2 + "\r\n elapsed time: " + elapsed2.ToString() +"\r\n Performance Hit: " + performancehit.ToString());
}
textBox1.AppendText("Performance Complete");
}
My results:
Assign Time start: 12/28/2011 9:39:19 PM Assign Time End: 12/28/2011 9:39:32 PM elapsed time: 00:00:13.6545000 NoAssign Time start: 12/28/2011 9:39:32 PM NoAssign Time End: 12/28/2011 9:39:46 PM elapsed time: 00:00:13.6080000
Performance Hit: 00:00:00.0465000
Assign Time start: 12/28/2011 9:39:46 PM Assign Time End: 12/28/2011 9:40:00 PM elapsed time: 00:00:13.7054000 NoAssign Time start: 12/28/2011 9:40:00 PM NoAssign Time End: 12/28/2011 9:40:13 PM elapsed time: 00:00:13.6400000 Performance Hit: 00:00:00.0654000
Assign Time start: 12/28/2011 9:40:13 PM Assign Time End: 12/28/2011 9:40:27 PM elapsed time: 00:00:13.6434000 NoAssign Time start: 12/28/2011 9:40:27 PM NoAssign Time End: 12/28/2011 9:40:41 PM elapsed time: 00:00:13.7122000
Performance Hit:
-00:00:00.0688000
Assign Time start: 12/28/2011 9:40:41 PM Assign Time End: 12/28/2011 9:40:54 PM elapsed time: 00:00:13.6544000 NoAssign Time start: 12/28/2011 9:40:54 PM NoAssign Time End: 12/28/2011 9:41:08 PM elapsed time: 00:00:13.6342000 Performance Hit: 00:00:00.0202000
Assign Time start: 12/28/2011 9:41:08 PM Assign Time End: 12/28/2011 9:41:22 PM elapsed time: 00:00:13.6788000 NoAssign Time start: 12/28/2011 9:41:22 PM NoAssign Time End: 12/28/2011 9:41:35 PM elapsed time: 00:00:13.6754000
Performance Hit: 00:00:00.0034000
Performance Complete
So it looks like there is a hit. However in the 3rd run - it was faster the other way. Perhaps something else was running on my pc for that time that may have affected this?
EDIT2
Doing some more digging on this. Found a very good article by Charlie Calvert here:
http://blogs.msdn.com/b/charlie/archive/2006/10/11/optimizing-c_2300_-string-performance.aspx
This has to do with the fact that
C# maintains something called an "intern table." This is a list of strings that are currently referenced. If a new string is created with code like that shown in lines 18 and 19, then the intern table is checked. If your string is already in there, then both variables will point at the same block of memory maintained by the intern table. The string is not duplicated.
I would gather that assuming the textbox data is stored as a string, and both my s1 and textbox1.text strings are the same, they will be referencing the same place in the memory. So at least we are not taking a hit on the memory.