tailieunhanh - Tìm hiểu C# 2005 Programmer’s Reference - chapter 7

Đây là giáo trình lập trình C# bằng tiếng Anh dành cho giáo viên, sinh viên chuyên ngành công nghệ thông tin tham khảo. | 118 CHAPTER 7 Strings Here s an example using StringBuilder StringBuilder sb new StringBuilder Hello World Conventional wisdom tells you to use StringBuilder for concatenating strings but that answer is too simplistic. In fact the official guidelines gives as good guidance as we re likely to find. The following is from the MSDN documentation for http en-us library The performance of a concatenation operation for a String or StringBuilder object depends on how often a memory allocation occurs. A String concatenation operation always allocates memory whereas a StringBuilder concatenation operation only allocates memory if the StringBuilder object buffer is too small to accommodate the new data. Consequently the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. In that case the individual concatenation operations might even be combined into a single operation by the compiler. A StringBuilder object is preferable for a concatenation operation if an arbitrary number of strings are concatenated for example if a loop concatenates a random number of strings of user input. Running my own tests basically confirms this strategy. StringBuilder is only faster once we re dealing with a lot of strings as Figure shows. Despite string append being much slower we re still talking about milliseconds either way. Number of Words String append----StringBuilder --StringBuilder Preallocate FIGURE Using StringBuilder doesn t matter for performance when there are relatively few strings. Concatenate Collection Items into a String 119 However a caveat All of this greatly depends on the size and number of your strings. Whenever performance is involved there is only one rule measure it. You also need to keep in mind the number of objects created when standard string concatenation is used a new string is created at each .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.