Created
March 19, 2018 21:25
-
-
Save Thealexbarney/1ef65271b88a8c20aa2428a3c1a95eef to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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