File tree 5 files changed +84
-0
lines changed
5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
4
4
5
5
| BlogPost | Publish Date |
6
6
| ---------------------------------------------------------------------------------------- | ------------ |
7
+ | [ Tail-Recursion - Explained with the Fibonacci series] ( TailRecursion/ ) | 13.05.2022 |
7
8
| [ Modal Dialog component with Bootstrap in Blazor] ( ModalDialogComponent/ ) | 19.04.2022 |
8
9
| [ 4 Different ways of creating an array] ( ArrayInitializePerformance/ ) | 14.04.2022 |
9
10
| [ Create a low allocation and faster StringBuilder - Span in Action] ( ValueStringBuilder/ ) | 03.04.2022 |
Original file line number Diff line number Diff line change
1
+ using BenchmarkDotNet . Attributes ;
2
+ using BenchmarkDotNet . Running ;
3
+
4
+ BenchmarkRunner . Run < Fibonacci > ( ) ;
5
+
6
+ public class Fibonacci
7
+ {
8
+ public const int FibonacciOf = 25 ;
9
+
10
+ [ Benchmark ( Baseline = true ) ]
11
+ public int FibonacciIterativeCall ( ) => FibonacciIterative ( FibonacciOf ) ;
12
+
13
+ [ Benchmark ]
14
+ public int FibonacciRecursiveCall ( ) => FibonacciRecursive ( FibonacciOf ) ;
15
+
16
+ [ Benchmark ]
17
+ public int FibonacciTailRecursiveCall ( ) => FibonacciTailRecursive ( FibonacciOf ) ;
18
+
19
+ private static int FibonacciIterative ( int n )
20
+ {
21
+ if ( n <= 1 ) return n ;
22
+
23
+ var ( previous , current ) = ( 0 , 1 ) ;
24
+ for ( var i = 2 ; i < n ; i ++ )
25
+ {
26
+ ( previous , current ) = ( current , current + previous ) ;
27
+ }
28
+
29
+ return current ;
30
+ }
31
+
32
+ private static int FibonacciRecursive ( int n )
33
+ {
34
+ if ( n <= 1 ) return n ;
35
+
36
+ return FibonacciRecursive ( n - 2 ) + FibonacciRecursive ( n - 1 ) ;
37
+ }
38
+
39
+ private static int FibonacciTailRecursive ( int n , int previous = 0 , int current = 1 )
40
+ {
41
+ if ( n == 0 )
42
+ return previous ;
43
+ if ( n == 1 )
44
+ return current ;
45
+
46
+ return FibonacciTailRecursive ( n - 1 , current , previous + current ) ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ # Tail-Recursion - Explained with the Fibonacci series
2
+
3
+ What is ** Tail-Recursion** ? We will discover this "special" form of recursion on the example of the * Fibonacci* series. Also we will check how much faster it is and why.
4
+
5
+ Found [ here] ( https://steven-giesel.com/blogPost/ccdbefd9-2875-49e6-929c-c5081d5b4d27 )
Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+
3
+ <PropertyGroup >
4
+ <OutputType >Exe</OutputType >
5
+ <TargetFramework >net6.0</TargetFramework >
6
+ <ImplicitUsings >enable</ImplicitUsings >
7
+ <Nullable >enable</Nullable >
8
+ </PropertyGroup >
9
+
10
+ <ItemGroup >
11
+ <PackageReference Include =" BenchmarkDotNet" Version =" 0.13.1" />
12
+ </ItemGroup >
13
+
14
+ </Project >
Original file line number Diff line number Diff line change
1
+
2
+ Microsoft Visual Studio Solution File, Format Version 12.00
3
+ Project ("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" ) = "TailRecursion" , "TailRecursion.csproj" , "{B3CFD7EA-8C7B-45FE-9AFD-A9AA8F3E7CC8}"
4
+ EndProject
5
+ Global
6
+ GlobalSection (SolutionConfigurationPlatforms ) = preSolution
7
+ Debug| Any CPU = Debug| Any CPU
8
+ Release| Any CPU = Release| Any CPU
9
+ EndGlobalSection
10
+ GlobalSection (ProjectConfigurationPlatforms ) = postSolution
11
+ {B3CFD7EA-8C7B-45FE-9AFD-A9AA8F3E7CC8} .Debug| Any CPU .ActiveCfg = Debug| Any CPU
12
+ {B3CFD7EA-8C7B-45FE-9AFD-A9AA8F3E7CC8} .Debug| Any CPU .Build .0 = Debug| Any CPU
13
+ {B3CFD7EA-8C7B-45FE-9AFD-A9AA8F3E7CC8} .Release| Any CPU .ActiveCfg = Release| Any CPU
14
+ {B3CFD7EA-8C7B-45FE-9AFD-A9AA8F3E7CC8} .Release| Any CPU .Build .0 = Release| Any CPU
15
+ EndGlobalSection
16
+ EndGlobal
You can’t perform that action at this time.
0 commit comments