Skip to content

Instantly share code, notes, and snippets.

@Postlagerkarte
Last active November 9, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Postlagerkarte/65fde9ea48b55e8dc6639d9b236bab97 to your computer and use it in GitHub Desktop.
Save Postlagerkarte/65fde9ea48b55e8dc6639d9b236bab97 to your computer and use it in GitHub Desktop.
Quick performance test
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ConsoleApp1
{
public class Test1
{
public static List<string> Input { get; set; } = new List<string>();
public static void Init(string[] args)
{
for (int x = 0; x < 10000000; x++)
{
Input.Add(RandomString(10));
}
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
[Benchmark]
public static void InputZip()
{
var result = Input.Zip(Input.Skip(1), (a, b) => ValueTuple.Create(a, b)).ToList();
}
[Benchmark]
public static void PairwiseExtension()
{
var pairs = Input.Pairwise().ToList();
}
[Benchmark]
public static void Stack()
{
Stack<string> stack = new Stack<string>(Input.ToArray().Reverse());
var result = new List<ValueTuple<string, string>>();
while (stack.Count > 1)
{
result.Add(new ValueTuple<string, string>(stack.Pop(), stack.Peek()));
}
}
[Benchmark]
public static void Queue()
{
var queue = new Queue<string>(Input);
var result = new List<ValueTuple<string, string>>();
while (queue.Count > 1)
{
result.Add(new ValueTuple<string, string>(queue.Dequeue(), queue.Peek()));
}
}
[Benchmark]
public static void ForLoop()
{
List<string[]> output = new List<string[]>();
for (int i = 0; i < Input.Count - 1; i++)
{
var pair = new string[2];
pair[0] = Input[i];
pair[1] = Input[i + 1];
output.Add(pair);
}
}
[Benchmark]
public static void TupleExtension()
{
var pairs = Input.Tuples(2).ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment