Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var p = new { FirstName = "Bill", LastName = "Gates" };
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
- Console.WriteLine(p.FirstName + " " + p.LastName);
- string.Format
- string.Format
- Bill Gates
- Console.WriteLine(p.FirstName + " " + p.LastName); took: 8ms - 30488 ticks
- Bill Gates
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: 0ms - 182 ticks
- Bill Gates
- Console.WriteLine(FirstName + " " + LastName); took: 5ms - 20335 ticks
- Bill Gates
- Console.WriteLine(FirstName + " " + LastName); took: 0ms - 156 ticks
- Bill Gates
- Console.WriteLine(FirstName + " " + LastName); took: 0ms - 122 ticks
- Bill Gates
- Console.WriteLine("{0} {1}", FirstName, LastName); took: 0ms - 181 ticks
- Bill Gates
- Console.WriteLine("{0} {1}", FirstName, LastName); took: 0ms - 122 ticks
- Bill Gates
- String.Concat(FirstName, " ", LastName); took: 0ms - 142 ticks
- Bill Gates
- String.Concat(FirstName, " ", LastName); took: 0ms - 117 ticks
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took (avg): 0ms - 689 ticks
- Console.WriteLine(p.FirstName + " " + p.LastName); took (avg): 0ms - 683 ticks
- Stopwatch s = new Stopwatch();
- var p = new { FirstName = "Bill", LastName = "Gates" };
- //First print to remove the initial cost
- Console.WriteLine(p.FirstName + " " + p.LastName);
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
- int n = 100000;
- long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;
- for (var i = 0; i < n; i++)
- {
- s.Start();
- Console.WriteLine(p.FirstName + " " + p.LastName);
- s.Stop();
- cElapsedMilliseconds += s.ElapsedMilliseconds;
- cElapsedTicks += s.ElapsedTicks;
- s.Reset();
- s.Start();
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
- s.Stop();
- fElapsedMilliseconds += s.ElapsedMilliseconds;
- fElapsedTicks += s.ElapsedTicks;
- s.Reset();
- }
- Console.Clear();
- Console.WriteLine("Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took (avg): " + (fElapsedMilliseconds / n) + "ms - " + (fElapsedTicks / n) + " ticks");
- Console.WriteLine("Console.WriteLine(p.FirstName + " " + p.LastName); took (avg): " + (cElapsedMilliseconds / n) + "ms - " + (cElapsedTicks / n) + " ticks");
- string str = "{0} {1} is my friend. {3}, {2} is my boss.".FormatWith(prop1,prop2,prop3,prop4);
- string name = String.Format(ApplicationStrings.General.InformalUserNameFormat,this.FirstName,this.LastName);
- System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
- var p = new { FirstName = "Bill", LastName = "Gates" };
- s.Start();
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
- s.Stop();
- Console.WriteLine("Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: " + s.ElapsedMilliseconds + "ms - " + s.ElapsedTicks + " ticks");
- s.Reset();
- s.Start();
- Console.WriteLine(p.FirstName + " " + p.LastName);
- s.Stop();
- Console.WriteLine("Console.WriteLine(p.FirstName + " " + p.LastName); took: " + s.ElapsedMilliseconds + "ms - " + s.ElapsedTicks + " ticks");
- Bill Gates
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: 2ms - 7280 ticks
- Bill Gates
- Console.WriteLine(p.FirstName + " " + p.LastName); took: 0ms - 67 ticks
- Console.WriteLine("User {0} accessed {1} on {2}.", user.Name, fileName, timestamp);
- vs
- Console.WriteLine("User " + user.Name + " accessed " + fileName + " on " + timestamp + ".");
- s.Start();
- for (var i = 0; i < n; i++)
- result = string.Concat(p.FirstName, " ", p.LastName);
- s.Stop();
- ceElapsedMilliseconds = s.ElapsedMilliseconds;
- ceElapsedTicks = s.ElapsedTicks;
- s.Reset();
- 1000000 x result = string.Format("{0} {1}", p.FirstName, p.LastName); took: 249ms - 3571621 ticks
- 1000000 x result = (p.FirstName + " " + p.LastName); took: 65ms - 944948 ticks
- 1000000 x result = string.Concat(p.FirstName, " ", p.LastName); took: 54ms - 780524 ticks
- Console.WriteLine(String.Concat(p.FirstName," ",p.LastName));
- class Program {
- static void Main(string[] args) {
- var p = new { FirstName = "Bill", LastName = "Gates" };
- var tests = new[] {
- new { Name = "Concat", Action = new Action(delegate() { string x = p.FirstName + " " + p.LastName; }) },
- new { Name = "Format", Action = new Action(delegate() { string x = string.Format("{0} {1}", p.FirstName, p.LastName); }) },
- new { Name = "StringBuilder", Action = new Action(delegate() {
- StringBuilder sb = new StringBuilder();
- sb.Append(p.FirstName);
- sb.Append(" ");
- sb.Append(p.LastName);
- string x = sb.ToString();
- }) }
- };
- var Watch = new Stopwatch();
- foreach (var t in tests) {
- for (int i = 0; i < 5; i++) {
- Watch.Reset();
- long Elapsed = ElapsedTicks(t.Action, Watch, 10000);
- Console.WriteLine(string.Format("{0}: {1} ticks", t.Name, Elapsed.ToString()));
- }
- }
- }
- public static long ElapsedTicks(Action ActionDelg, Stopwatch Watch, int Iterations) {
- Watch.Start();
- for (int i = 0; i < Iterations; i++) {
- ActionDelg();
- }
- Watch.Stop();
- return Watch.ElapsedTicks / Iterations;
- }
- }
- Console.WriteLine("User {0} accessed {1} on {2}.",
- user.Name, fileName, timestamp);
- Console.WriteLine("User {0} accessed {1} on {2}.",
- user.Name, fileName, timestamp);
- Console.WriteLine("User " + user.Name + " accessed " + fileName +
- " on " + timestamp + ".");
- Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
- Console.WriteLine(p.FirstName + " " + p.LastName);
Add Comment
Please, Sign In to add comment