Created
September 12, 2012 02:47
-
-
Save davidfowl/3703926 to your computer and use it in GitHub Desktop.
Performance Showdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var cursors = new List<Cursor>(); | |
cursors.Add(new Cursor | |
{ | |
EscapedKey = "MyHub", | |
Id = 0 | |
}); | |
cursors.Add(new Cursor | |
{ | |
EscapedKey = "MyHub2", | |
Id = 1 | |
}); | |
cursors.Add(new Cursor | |
{ | |
EscapedKey = "MyHub.Group", | |
Id = 0 | |
}); | |
int iterations = 1000000; | |
var sw = Stopwatch.StartNew(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
MakeCursorJoin(cursors); | |
} | |
sw.Stop(); | |
Console.WriteLine("Concat {0}", sw.Elapsed); | |
sw = Stopwatch.StartNew(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
MakeCursorStringBuilder(cursors); | |
} | |
sw.Stop(); | |
Console.WriteLine("StringBuilder {0}", sw.Elapsed); | |
} | |
private static string MakeCursorJoin(IList<Cursor> cursors) | |
{ | |
var serialized = new string[cursors.Count]; | |
for (int i = 0; i < cursors.Count; i++) | |
{ | |
serialized[i] = cursors[i].EscapedKey + ',' + cursors[i].Id; | |
} | |
return String.Join("|", serialized); | |
} | |
private static string MakeCursorStringBuilder(IList<Cursor> cursors) | |
{ | |
var sb = new StringBuilder(); | |
for (int i = 0; i < cursors.Count; i++) | |
{ | |
if (i > 0) | |
{ | |
sb.Append('|'); | |
} | |
sb.Append(cursors[i].EscapedKey) | |
.Append(',') | |
.Append(cursors[i].Id); | |
} | |
return sb.ToString(); | |
} | |
} | |
public class Cursor | |
{ | |
public string EscapedKey { get; set; } | |
public ulong Id { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A quick test to share what I'm seeing (differently). Code was executed using LINQPad 4.42.05 beta (anyCPU) and LINQPad 4.42.01 (x86), respectively, and optimizations were turned on.
Results (x64)
Results (x86)
Test Configuration