// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; namespace E { public class MainClass { public static string MultiAppendString(int n) { string res = ""; for(int i = 0; i < n; i++) { res += 'A'; } return res; } public static string MultiAppendStringBuilder(int n) { StringBuilder res = new StringBuilder(); for(int i = 0; i < n; i++) { res.Append('A'); } return res.ToString(); } private const int N = 1000000; public static void Test(int n) { string res1,res2; long t1 = DateTime.Now.Ticks; for(int i = 0; i < N/n; i++) { res1 = MultiAppendString(n); } long t2 = DateTime.Now.Ticks; long t3 = DateTime.Now.Ticks; for(int i = 0; i < N/n; i++) { res2 = MultiAppendStringBuilder(n); } long t4 = DateTime.Now.Ticks; Console.WriteLine(String.Format("{0,4} {1,5:f2}", n, (t2-t1)/(double)(t4-t3))); } public static void Main(string[] args) { if(MultiAppendString(10)!="AAAAAAAAAA") { Console.WriteLine("Oops"); Environment.Exit(0); } if(MultiAppendStringBuilder(10)!="AAAAAAAAAA") { Console.WriteLine("Oops"); Environment.Exit(0); } int n = 1; while(n < 10000) { Test(n); n *= 2; } } } }