-
-
Save olmobrutall/fa738d2fe8bf48a7766b 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.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace SkipTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
for (int i = 1; i < 100000; i+=1) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
int sum = Enumerable.Range(0, i * 100).Sum(n => { /*Thread.Sleep(1);*/ return n; }); | |
sw.Stop(); | |
var iterative = sw.ElapsedMilliseconds; | |
sw.Reset(); | |
sw.Start(); | |
sum = RecursiveSum(Enumerable.Range(0, i * 100)); | |
sw.Stop(); | |
var recursive = sw.ElapsedMilliseconds; | |
Console.WriteLine("Iterative\t{0}\tRecursive\t{1}", iterative / i, recursive / i); | |
} | |
} | |
private static int RecursiveSum(IEnumerable<int> enumerable) | |
{ | |
if (!enumerable.Any()) | |
return 0; | |
return enumerable.First() + RecursiveSum(enumerable.Skip(1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment