0

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.

Sam
  • 946
  • 3
  • 11
  • 22
  • 2
    Have you tried creating 3.8 million textboxes and measuring? – Matt Greer Dec 29 '11 at 01:39
  • 2
    You have a function with 4 million arguments? I'm impressed. – Mike Dunlavey Dec 29 '11 at 02:01
  • :) this is a theoretical question. – Sam Dec 29 '11 at 02:14
  • There is a trade-off here, readability vs. performance. The performance hit can only be guessed at until you write the code and benchmark it! – vdbuilder Dec 29 '11 at 02:19
  • Sadly a rather dubious benchmark - at best. REad [this](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) first.. yes it's java, yes basically the same thing's apply still – Voo Dec 29 '11 at 05:30
  • Closed as NC until you've performed some benchmarks (not necessarily 3.8M, but something with a large number of textboxes). – casperOne Dec 30 '11 at 06:47
  • I'm probably just a sample of one, but I don't like to create any more text boxes or other controls than can be visible at one time to a person looking at a display screen. Of course, that's the default behavior of *[differential execution](http://stackoverflow.com/questions/tagged/differential-execution)*. – Mike Dunlavey Jan 01 '12 at 18:13

1 Answers1

1

I'd say a 0.5% performance hit (0.0688 seconds out of 13.6 total seconds) is not worth reducing readability and maintainability by removing variables, even 3.8MM of them :).

D Stanley
  • 149,601
  • 11
  • 178
  • 240