Created
March 8, 2020 21:30
-
-
Save sandiks/7ded218323c96dc36e6de1455ada8675 to your computer and use it in GitHub Desktop.
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.Diagnostics; | |
using System.Linq; | |
namespace Sample22 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int N = 100_000_000; | |
var data = Generate(N); | |
var sw = Stopwatch.StartNew(); | |
var res1 = TestFor(data); | |
System.Console.WriteLine($"min: {res1} elapsed: {sw.Elapsed}"); | |
sw = Stopwatch.StartNew(); | |
var res2 = TestLinq(data); | |
System.Console.WriteLine($"min: {res2} elapsed: {sw.Elapsed}"); | |
} | |
private static int TestFor(int[] data) | |
{ | |
int min = Int32.MaxValue; | |
for (int i = 0; i < data.Length; i++) | |
{ | |
if (data[i]>0 && data[i]<min) min = data[i]; | |
} | |
return min; | |
} | |
private static int TestLinq(int[] data) | |
{ | |
return data.Where(x =>x>0).Min(); | |
} | |
private static int[] Generate(int N) | |
{ | |
var data = new int[N]; | |
var randNum = new Random(); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
data[i] = randNum.Next(1, 1000000) - 50000; | |
} | |
return data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment