Skip to content

Instantly share code, notes, and snippets.

@olmobrutall
Created April 16, 2014 20:58
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 olmobrutall/fa738d2fe8bf48a7766b to your computer and use it in GitHub Desktop.
Save olmobrutall/fa738d2fe8bf48a7766b to your computer and use it in GitHub Desktop.
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