Results 1 to 5 of 5

Thread: Which is faster that VB will read the code?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    95

    Question Which is faster that VB will read the code?

    Hi guys! Good eve/day!

    I have a question to ask hope you can explain to me in easy way (sorry for my grammar)

    Which is more faster?

    The classic way?

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    myConnection.Open()
    ' codes here...
    myConnection.Close()
    End Sub
    or the Call a Function?

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    myConnection.Open()
    Class.Function()
    myConnection.Close()
    End Sub
    What are the Advantages and Disadvantages of these two?
    Last edited by MeSH; Feb 14th, 2015 at 12:05 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: Which is faster that VB will read the code?

    If your connection implements the IDisposable Interface then it's best to use the Using statement:

    Code:
    Using myConnection As SomeObject = New SomeObject
        'Utilize myConnection here
    End Using
    
    'myConnection no longer exists here

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    95

    Re: Which is faster that VB will read the code?

    Quote Originally Posted by dday9 View Post
    If your connection implements the IDisposable Interface then it's best to use the Using statement:

    Code:
    Using myConnection As SomeObject = New SomeObject
        'Utilize myConnection here
    End Using
    
    'myConnection no longer exists here
    I have many codes in our system.... it's like hundreds lines of codes in a form.... I read some article about functions and I convert all my codes in the function so that it's more neat when it looks... when I put my codes in a function it is more faster to read my codes than doing the classic way?

    this is sample codes the time that I did not put on a function

    Code:
    myConnection.Open()
            Try
                'Output from TIME database
                database = "SELECT STUD_ID, LOGIN_TIME FROM daily_attendance WHERE (STUD_ID = '" & TextBox1.Text & "')"
                Dim cmd As OleDbCommand = New OleDbCommand(database, myConnection)
                dataReader = cmd.ExecuteReader
    
                While dataReader.Read()
                    prevTime = dataReader("LOGIN_TIME").ToString
                    studIdHolder = dataReader("STUD_ID").ToString
                End While
                myConnection.Close()
    
                'UPDATE DATABASE
                'If studIdHolder = TextBox1.Text Then
                If String.Equals(TextBox1.Text, studIdHolder) Then
                    If timeMark >= 30 Then ' 30 mins mark
                        myConnection.Open()
    
                        database = "UPDATE daily_attendance SET LOGIN_TIME =@time WHERE STUD_ID =@studid"
                        Using cmd2 As OleDbCommand = New OleDbCommand(database, myConnection)
                            cmd2.Parameters.AddWithValue("@time", curTime)
                            cmd2.Parameters.AddWithValue("@studid", TextBox1.Text)
                            cmd2.ExecuteNonQuery()
                        End Using
                        MessageBox.Show("Login Successful!")
    
                        If courseIdentifier = "10" Or courseIdentifier = "11" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 1"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount1 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount1 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 1"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount1)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "12" Or courseIdentifier = "13" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 2"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount2 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount2 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 2"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount2)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "14" Or courseIdentifier = "15" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 3"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount3 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount3 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 3"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount3)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "16" Or courseIdentifier = "17" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 4"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount4 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount4 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 4"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount4)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "18" Or courseIdentifier = "19" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 5"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount5 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount5 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 5"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount5)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "20" Or courseIdentifier = "21" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 6"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount6 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount6 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 6"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount7)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "22" Or courseIdentifier = "23" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 7"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount7 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount7 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 7"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount7)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "24" Or courseIdentifier = "25" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 8"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount8 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount8 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 8"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount8)
                                courseCMD.ExecuteNonQuery()
                            End Using
                        ElseIf courseIdentifier = "26" Or courseIdentifier = "27" Then
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 9"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount9 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount9 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 9"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount9)
                                courseCMD.ExecuteNonQuery()
                            End Using
    
                    and so on...

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,500

    Re: Which is faster that VB will read the code?

    The code runs at the same speed. Calling a function takes a tiny amount of time, so you should ignore it unless you are doing it in a loop that you call millions of times.


    There are much bigger speed issues in your code, the biggest of which is that you make two database calls each time when you only need to make one. Instead of code like this:
    Code:
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 1"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount1 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount1 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 1"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount1)
                                courseCMD.ExecuteNonQuery()
                            End Using
    ...you can do this:
    Code:
                            database = "UPDATE course SET course_count = course_count + 1 WHERE course_id = @courseID"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue(@courseID, 1)
                                courseCMD.ExecuteNonQuery()
                            End Using
    Once you have that, you can make the rest of the code much shorter (so easier to write/read/maintain), by using a variable to set @courseID , and set the variable based on a Select Case (a faster version of If when checking a variable against multiple values). As such, the entire code from If courseIdentifier = "10" onwards can become:
    Code:
                            'work out the course id
                            Dim courseID as Integer
                            Select Case courseIdentifier 
                            Case "10", "11"
                               courseID = 1
                            Case "12", "13"
                               courseID = 2
                            Case "14", "15"
                               courseID = 3
                    and so on...
                            End Select
    
                            'increase the count for the course
                            database = "UPDATE course SET course_count = course_count + 1 WHERE course_id = @courseID"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue(@courseID, courseID)
                                courseCMD.ExecuteNonQuery()
                            End Using

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    95

    Re: Which is faster that VB will read the code?

    Quote Originally Posted by si_the_geek View Post
    The code runs at the same speed. Calling a function takes a tiny amount of time, so you should ignore it unless you are doing it in a loop that you call millions of times.


    There are much bigger speed issues in your code, the biggest of which is that you make two database calls each time when you only need to make one. Instead of code like this:
    Code:
                            database = "SELECT COURSE_COUNT FROM COURSE WHERE COURSE_ID = 1"
                            Dim countCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                            dataReader = countCMD.ExecuteReader
    
                            While dataReader.Read()
                                addCount1 = dataReader("COURSE_COUNT").ToString
                            End While
    
                            addCount1 += 1
                            database = "UPDATE course SET course_count =@count WHERE course_id = 1"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue("@count", addCount1)
                                courseCMD.ExecuteNonQuery()
                            End Using
    ...you can do this:
    Code:
                            database = "UPDATE course SET course_count = course_count + 1 WHERE course_id = @courseID"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue(@courseID, 1)
                                courseCMD.ExecuteNonQuery()
                            End Using
    Once you have that, you can make the rest of the code much shorter (so easier to write/read/maintain), by using a variable to set @courseID , and set the variable based on a Select Case (a faster version of If when checking a variable against multiple values). As such, the entire code from If courseIdentifier = "10" onwards can become:
    Code:
                            'work out the course id
                            Dim courseID as Integer
                            Select Case courseIdentifier 
                            Case "10", "11"
                               courseID = 1
                            Case "12", "13"
                               courseID = 2
                            Case "14", "15"
                               courseID = 3
                    and so on...
                            End Select
    
                            'increase the count for the course
                            database = "UPDATE course SET course_count = course_count + 1 WHERE course_id = @courseID"
                            Using courseCMD As OleDbCommand = New OleDbCommand(database, myConnection)
                                courseCMD.Parameters.AddWithValue(@courseID, courseID)
                                courseCMD.ExecuteNonQuery()
                            End Using
    sir your code is working for daily_attendance only but it not add to the COURSE database (by the way I change COURSE database to LIBRARY_CLERK.)

    this is I want:

    for daily_attendance that is the record for daily report and for library_clerk database is for monthly report... so every record I put in the daily_attendance it will add also in library_clerk

    but the code that I edit base in your suggestion it only insert and update in daily_attendance but the library_clerk is empty

    EDIT:

    Please don't mind my reply above...

    It's working now!

    thanks sir si_the_geek!

    now my codes are more shorter and it's easy for me now to code the PROCESS SPECIFICATION (pseudo code) in our thesis
    Last edited by MeSH; Feb 14th, 2015 at 11:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width