Tuesday, February 19, 2013

The multiplication of loops is the trap of programming

The multiplication of loops is the trap of programming


In the previous article, we have made ​​a password generator essentially consists of numbers based on two different approaches: hard-coding consists of many lines of code proved to be the fastest way. Code with recursion is made with fewer lines of code, but has been slower in the final. Although one may think that the hard coding is faster in terms of performance, it is actually the least flexible. Look more closely at the following piece of code. Go directly to the end of the computer code for an explanation.The multiplication of loops is the trap of programming




    Private Function CheckLetter1()
        Dim index1 As Integer
        Dim index2 As Integer
        Dim index3 As Integer
        Dim index4 As Integer
        Dim index5 As Integer
        Dim index6 As Integer
        Dim found As Boolean
        Dim date1 As Date
        Dim date2 As Date
        Dim checker As String
        date1 = DateTime.Now
        found = False

        If NumericUpDown1.Value >= 1 Then
            'look for first draw
            For index1 = 0 To 25 Step 1 'a - z
                checker = Microsoft.VisualBasic.ChrW(97 + index1)
                ListBox1.Items.Add(checker)
                If TextBox1.Text = checker Then
                    TextBox2.Text = checker
                    found = True
                End If
            Next
            For index1 = 0 To 9 Step 1 '0 - 9
                checker = Microsoft.VisualBasic.ChrW(48 + index1)
                ListBox1.Items.Add(checker)
                If TextBox1.Text = checker Then
                    TextBox2.Text = checker
                    found = True
                End If
            Next

            For index1 = 0 To 25 Step 1 'A - Z
                checker = Microsoft.VisualBasic.ChrW(65 + index1)
                ListBox1.Items.Add(checker)
                If TextBox1.Text = checker Then
                    TextBox2.Text = checker
                    found = True
                End If
            Next
        End If


        If NumericUpDown1.Value >= 2 Then
            'look for numbers 2nd level
            For index1 = 0 To 9 Step 1 ' 0 - 9
                '''
                For index2 = 0 To 25 Step 1 'a - z
                    checker = Microsoft.VisualBasic.ChrW(48 + index1) & Microsoft.VisualBasic.ChrW(97 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                For index2 = 0 To 9 Step 1 '0 - 9
                    checker = Microsoft.VisualBasic.ChrW(48 + index1) & Microsoft.VisualBasic.ChrW(48 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next

                For index2 = 0 To 25 Step 1 'A - Z
                    checker = Microsoft.VisualBasic.ChrW(48 + index1) & Microsoft.VisualBasic.ChrW(65 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                ''''

            Next
            For index1 = 0 To 25 Step 1 ' 0 - 9
                '''
                For index2 = 0 To 25 Step 1 'a - z
                    checker = Microsoft.VisualBasic.ChrW(97 + index1) & Microsoft.VisualBasic.ChrW(97 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                For index2 = 0 To 9 Step 1 '0 - 9
                    checker = Microsoft.VisualBasic.ChrW(97 + index1) & Microsoft.VisualBasic.ChrW(48 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next

                For index2 = 0 To 25 Step 1 'A - Z
                    checker = Microsoft.VisualBasic.ChrW(97 + index1) & Microsoft.VisualBasic.ChrW(65 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                ''''

            Next
            For index1 = 0 To 25 Step 1 ' 0 - 9
                '''
                For index2 = 0 To 25 Step 1 'a - z
                    checker = Microsoft.VisualBasic.ChrW(65 + index1) & Microsoft.VisualBasic.ChrW(97 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                For index2 = 0 To 9 Step 1 '0 - 9
                    checker = Microsoft.VisualBasic.ChrW(65 + index1) & Microsoft.VisualBasic.ChrW(48 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next

                For index2 = 0 To 25 Step 1 'A - Z
                    checker = Microsoft.VisualBasic.ChrW(65 + index1) & Microsoft.VisualBasic.ChrW(65 + index2)
                    ListBox1.Items.Add(checker)
                    If TextBox1.Text = checker Then
                        TextBox2.Text = checker
                        found = True
                    End If
                Next
                ''''

            Next
        End If

   date2 = DateTime.Now
        '        date2.Subtract(date1).ToString
        ToolStripStatusLabel1.Text = "Finished. " & date2.Subtract(date1).ToString & " and ListBox1 Count: " & ListBox1.Items.Count
        Return 0
    End Function




Function to generate the possible combinations of 0-9 and the letters of the alphabet in upper and lower case. After the function product with the same principle combinations along a length of password of two characters. We note that the first part of the code correctly produce the 62 possible combinations. We also note that the second part of the code also produces all possible combinations of passwords to two characters, but with an impressive amount of computer code. We guess pretty quickly that the complexity becomes ultra impressive when it comes time to encode a password to 3 characters long.
So what should we learn from this approach? Hard-coding the code for calculations of this type is very heavy and the final few ingenious. It is unthinkable not to use the power of the computer to get the desired result.

Read the previous article in the series of this article: Basic recursivity for password
Read the first series of this item : Password programming for beginners
Dans le prochain article : N/A
Download the sample project  : : PasswordGame.zip


No comments:

Post a Comment