Commits

Devin Martin  committed 6c5e07f

memoized example

  • Participants
  • Parent commits e440b7d

Comments (0)

Files changed (2)

File FlipSideOut.Linqpad/FlipSideOut.Linqpad.csproj

   </ItemGroup>
   <ItemGroup>
     <None Include="FlipSideOut.Linqpad.nuspec" />
+    <None Include="linqpad-samples\Functional\Memoize.linq" />
     <None Include="linqpad-samples\Histograms\NonUniformDistributionHistograms.linq" />
   </ItemGroup>
   <ItemGroup />

File FlipSideOut.Linqpad/linqpad-samples/Functional/Memoize.linq

+<Query Kind="Statements">
+  <NuGetReference>FlipSideOut.Linqpad</NuGetReference>
+  <Namespace>FlipSideOut.Linqpad</Namespace>
+</Query>
+
+Func<long, long> fib_recursive = null;
+Func<long, long> fibMemoized = null;
+
+fib_recursive = n => n > 2 ? fib_recursive(n - 1) + fib_recursive(n - 2) : 1;
+fibMemoized = Functional.Memoize<long, long>(n => n > 2 ? fibMemoized(n - 1) + fibMemoized(n - 2) : 1);
+
+var nonMemoizedTimer = new Stopwatch();
+nonMemoizedTimer.Start();
+fib_recursive(38).Dump("Recurseive Answer");
+nonMemoizedTimer.Stop();
+nonMemoizedTimer.Elapsed.Dump("Recursive");
+
+var memoizedTimer = new Stopwatch();
+memoizedTimer.Start();
+fibMemoized(38).Dump("Memoized Answer");
+memoizedTimer.Stop();
+memoizedTimer.Elapsed.Dump("Memoized");