fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace ConsoleApplication1 {
  8. class Program {
  9. static void Main( string[] args ) {
  10. Stopwatch watch;
  11.  
  12. string a, b, c, d, e, f, g, h;
  13. SetString( out a , out b , out c , out d , out e , out f , out g , out h );
  14.  
  15. int count = 100000;
  16.  
  17. for( int loop = 0 ; loop < 4 ; loop++ ) {
  18. // +連結
  19. watch = Stopwatch.StartNew();
  20. string[] s = new string[count];
  21. for( int i = 0 ; i < count ; i++ ) {
  22. s[i] = a + b + c + d + e + f + g + h;
  23. }
  24. watch.Stop();
  25. Console.WriteLine( "+:" + watch.ElapsedMilliseconds );
  26.  
  27. // StringBuilder連結
  28. watch = Stopwatch.StartNew();
  29. StringBuilder bulder = new StringBuilder();
  30. for( int i = 0 ; i < count ; i++ ) {
  31. bulder.Append( a ).Append( b ).Append( c ).Append( d ).Append( e ).Append( f ).Append( g ).Append( h );
  32. // このループでは、文字列を追加するのみ。
  33. }
  34. string ret = bulder.ToString(); // 文字列操作を終えてから文字列(string)を生成する。
  35. watch.Stop();
  36. Console.WriteLine( "StringBuilder:" + watch.ElapsedMilliseconds );
  37. }
  38.  
  39. Console.ReadLine();
  40. }
  41.  
  42. [System.Runtime.CompilerServices.MethodImpl( System.Runtime.CompilerServices.MethodImplOptions.NoInlining )]
  43. static void SetString( out string a , out string b , out string c , out string d , out string e , out string f , out string g , out string h ) {
  44. a = "AAAAAAAAAAAAAAAAA";
  45. b = "BBBBBBBBBBBBBBBBBBBBBBBB";
  46. c = "CCCCCCCCCCCCCCCCCCCC";
  47. d = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
  48. e = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
  49. f = "FFFFFFFFFFFFFFFFFFFFFF";
  50. g = "GGGGGGGGGGGGGGGGG";
  51. h = "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH";
  52. }
  53.  
  54. }
  55. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty