拼接字符串对于程序员来说是再熟悉不过的东西,可是这个东西用了这么确不知道那个性能高些,于是写了点代码来验证。
1 string str = ""; 2 3 DateTime time3 = DateTime.Now; 4 for (int i = 0; i < 200000; i++) 5 { 6 str = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}{19}{20}{21}{22}{23}{24}{25}{26}{27}{28}{29}{30}{31}{32}{33}{34}{35}", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); 7 } 8 DateTime time4 = DateTime.Now; 9 Console.WriteLine(time4.Ticks - time3.Ticks); 10 Console.WriteLine(str); 11 12 DateTime time1 = DateTime.Now; 13 for (int i = 0; i < 200000; i++) 14 { 15 str = "a" + "b" + "c" + "d" + "e" + "f" + "g" + "h" + "i" + "j" + "k" + "l" + "m" + "n" + "o" + "p" + "q" + "r" + "s" + "t" + "u" + "v" + "w" + "x" + "y" + "z" + "1" + "2" + "3" + "4" + "5" + "6" + "7" + "8" + "9" + "11"; 16 } 17 DateTime time2 = DateTime.Now; 18 Console.WriteLine(time2.Ticks - time1.Ticks); 19 Console.WriteLine(str); 20 21 Console.Read();
结果一目了然
时间: 2024-10-11 00:06:02