Đang chuẩn bị liên kết để tải về tài liệu:
Professional Visual Basic 2010 and .neT 4 phần 2

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Mã này không hoạt động tốt. Đối với mỗi hoạt động chuyển nhượng trên biến strMyString, hệ thống phân bổ một bộ nhớ đệm mới dựa vào kích thước của chuỗi mới, và sao chép cả giá trị hiện tại của strMyString và văn bản mới được nối. Hệ thống sau đó giải phóng bộ nhớ trước đó phải được khai hoang bởi những người thu gom rác. | 90 I CHAPTER 2 OBJECTS AND VISUAL BASIC TextBoxl.Text Time to concatenate strings Now .Subtract start .TotalMilliseconds .ToString String length strRedo.Length.ToString TextBox1.Text line Environment.NewLine Code snippet from Forml This code does not perform well. For each assignment operation on the strMyString variable the system allocates a new memory buffer based on the size of the new string and copies both the current value of strMyString and the new text that is to be appended. The system then frees the previous memory that must be reclaimed by the garbage collector. As this loop continues the new memory allocation requires a larger chunk of memory. Therefore operations such as this can take a long time. To illustrate this you ll note that the code captures the start time before doing the 10 000 concatenations and then within the print statement uses the DateTime.Subtract method to get the difference. That difference is returned as an object of type Timespan between the start time and the print time. This difference is then expressed in milliseconds refer to Figure 2-8 . However .NET offers an alternative in the System.Text.StringBuilder object shown in the following snippet start Now Dim strBuilder New System.Text.StringBuilder A simple string For index 1 To 1000000 1 million times. strBuilder.Append Making a much larger string Next TextBox1.Text Time to concatenate strings Now .Subtract start .TotalMilliseconds .ToString String length strBuilder.ToString .Length.ToString TextBox1.Text line Environment.NewLine End Sub Code snippet from Forml The preceding code works with strings but does not use the String class. The .NET class library contains the System.Text.StringBuilder class which performs better when strings will be edited repeatedly. This class does not store strings in the conventional manner it stores them as individual characters with code in place to manage the ordering of those characters. Thus editing or appending more characters does not .