Peter.BoyWonder
New member
So heres the Deal.
I was just messing around and trying to implement a simple recursive function that calculates N! (n factorial). I was also going to write one that does it iteratively and I was going to time the two to figure out which was faster (No BIG O notation here). I have the function working, thats not the problem. The problem is that the Timing aspect doesn't unless I set breakpoints and debug very slowly. Surely there is no way that the algorithm runs so fast that the elapsed time can't be calculated....right? What am I doing wrong? It appears so simple!
Here is the
I was just messing around and trying to implement a simple recursive function that calculates N! (n factorial). I was also going to write one that does it iteratively and I was going to time the two to figure out which was faster (No BIG O notation here). I have the function working, thats not the problem. The problem is that the Timing aspect doesn't unless I set breakpoints and debug very slowly. Surely there is no way that the algorithm runs so fast that the elapsed time can't be calculated....right? What am I doing wrong? It appears so simple!
Here is the
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] butCalculate_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] butCalculate.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Input to calculate N![/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] n [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](TextBox1.Text)[/SIZE]
[SIZE=2][COLOR=#008000]'Capture the time right before the algorithm runs[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] starttime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = DateTime.Now()[/SIZE]
[SIZE=2][COLOR=#008000]'Run the N! algorithm[/COLOR][/SIZE]
[SIZE=2]TextBox2.Text = Nfactorial(n)[/SIZE]
[SIZE=2][COLOR=#008000]'Capture the time after the algorithm runs[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Endtime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = DateTime.Now[/SIZE]
[SIZE=2][COLOR=#008000]'Calculate the elapsed time of the algorithm running[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] span [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = Endtime.Subtract(starttime)[/SIZE]
[SIZE=2][COLOR=#008000]'Output the elapsed time[/COLOR][/SIZE]
[SIZE=2]MessageBox.Show(span.Milliseconds.ToString)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] Nfactorial([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] N [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] N > 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]N = N * Nfactorial(N - 1)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] N[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
Last edited by a moderator: