When the author develops a small program, I encountered a requirement, that is, open the designated text content, judge the line, and then display it in the Text Control.
At first, the author used
While (streamReader.peek ()> 0)
{
...
TextBox1.text = strline;
}
StreamReader.close ();
There is no error during operation, but the efficiency is low in loading data, the record of load 1000 lines actually takes about 15 seconds, which greatly affects the use.
During the debugging process, the author discovered that the streamreader.open function call does not take time consumption, and the time consumption is less than 1 second, and therefore, the time-consuming operation is locked in TextBox1.text = Strline, guess, maybe TextBox1.text is assigned a certain amount of memory during the declaration, and may need to redistribute memory during each assignment, because the space occupied by the TextBox1 control is relatively large, so the re-allocation of the memory accounts for most time.
In order to confirm this conjecture, the author modified the program to:
String strdata = "";
While (streamReader.peek ()> 0)
{
...
StrData = strline;
}
TextBox1.text = strdata;
StreamReader.close ();
After the debug operation, the data file that loads 1000 rows of records takes only about 1 second or so.
This case description, when the control, class properties need to be incremented, it is best to assign a variable first, then assign the value of the variable to the control, class properties to avoid frequent modification of memory due to frequent modification controls and class properties. Leading to low efficiency.