Advertisement
Guest User

Untitled

a guest
Apr 27th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace ConsoleApplication2 {
  8.     class Program {
  9.         static string Concat(string s) {
  10.             return "for" + s + "bar" + s + "0123456789" + s + "01234567890123456789" + s + "01234567890123456789";
  11.         }
  12.  
  13.         static string Builder(string s) {
  14.             StringBuilder sb = new StringBuilder();
  15.             sb.Append("for");
  16.             sb.Append(s);
  17.             sb.Append("bar");
  18.             sb.Append(s);
  19.             sb.Append("0123456789");
  20.             sb.Append(s);
  21.             sb.Append("01234567890123456789");
  22.             sb.Append(s);
  23.             sb.Append("01234567890123456789");
  24.             return sb.ToString();
  25.         }
  26.  
  27.         static string Format(string s) {
  28.             return string.Format("for{0}bar{1}0123456789{2}01234567890123456789{3}01234567890123456789", s, s, s, s);
  29.         }
  30.  
  31.         static void Test(Func<string, string> f) {
  32.             Stopwatch sw = new Stopwatch();
  33.             sw.Start();
  34.             for (int m = 0; m < 1000000; m++) {
  35.                 var s = f("blablabla");
  36.             }
  37.             sw.Stop();
  38.             Console.WriteLine("{0}: {1} ms", f.GetType().Name, sw.ElapsedMilliseconds);
  39.         }
  40.  
  41.         static void Main(string[] args) {
  42.             foreach (var method in new Func<string, string>[] { Concat, Builder, Format }) {
  43.                 Test(method);
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement