Skip to content

Instantly share code, notes, and snippets.

@Thealexbarney
Created March 19, 2018 21:25
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 Thealexbarney/1ef65271b88a8c20aa2428a3c1a95eef to your computer and use it in GitHub Desktop.
Save Thealexbarney/1ef65271b88a8c20aa2428a3c1a95eef to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace Bench
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Bench>();
}
}
public class Bench
{
private const int iterations = 10000000;
private List<int> A = new List<int>(iterations);
private List<int> B = new List<int>(iterations);
public Bench()
{
var rand = new Random();
for (int i = 0; i < iterations; i++)
{
A.Add(rand.Next(10000));
B.Add(rand.Next(10000));
}
}
[Benchmark]
public IList<int> RunInterface() => MultVectorsI(A, B);
[Benchmark]
public List<int> RunNoInterface() => MultVectors(A, B);
public List<int> MultVectors(List<int> a, List<int> b)
{
int length = a.Count;
var c = new List<int>(length);
for (int i = 0; i < length; i++)
{
double valA = a[i];
double valB = b[i];
c.Add((int)Math.Sqrt(valA * valA + valB * valB));
}
return c;
}
public IList<int> MultVectorsI(IList<int> a, IList<int> b)
{
int length = a.Count;
var c = new List<int>(length);
for (int i = 0; i < length; i++)
{
double valA = a[i];
double valB = b[i];
c.Add((int)Math.Sqrt(valA * valA + valB * valB));
}
return c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment