none
修改一些程式碼 RRS feed

  • 問題

  • 請問下列的程式碼,若考慮到效能的話,是不是可以做什麼修改?
    (ComboBox1.Text會<1000000)

    Public Class Form1
        Dim i As Long
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PictureBox1.Refresh()
            Select Case ComboBox1.Text
                Case "點"
                    For i = 1 To Val(ComboBox2.Text)
                        Randomize()
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        Dim x1 As Integer = Int(Rnd() * 550)
                        Dim x2 As Integer = Int(Rnd() * 550)
                        formGraphics1.DrawLine(myPen1, x1, x2 - 1, x1 - 1, x2)
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "平行線"
                    For i = 1 To Val(ComboBox2.Text)
                        Randomize()
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        Dim x1 As Integer = Int(Rnd() * 550)
                        Dim x2 As Integer = Int(Rnd() * 550)
                        formGraphics1.DrawLine(myPen1, x1, x2 - 1, x2, x1 - 1)
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "輻射線"
                    For i = 1 To Val(ComboBox2.Text)
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        formGraphics1.DrawLine(myPen1, 280, 280, Int(Rnd() * 550), Int(Rnd() * 550))
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "任意線"
                    For i = 1 To Val(ComboBox2.Text)
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        formGraphics1.DrawLine(myPen1, Int(Rnd() * 550), Int(Rnd() * 550), Int(Rnd() * 550), Int(Rnd() * 550))
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "任意橢圓"
                    For i = 1 To Val(ComboBox2.Text)
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        Dim x1 As Integer = Int(Rnd() * 550)
                        Dim x2 As Integer = Int(Rnd() * 550)
                        formGraphics1.DrawEllipse(myPen1, x1, x2, Int(Rnd() * 550) - x1, Int(Rnd() * 550) - x2)
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "任意圓"
                    For i = 1 To Val(ComboBox2.Text)
                        Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()
                        Dim x As Integer = Int(Rnd() * 550)
                        Dim y1 As Integer = Int(Rnd() * 550)
                        Dim y2 As Integer = Int(Rnd() * 550)
                        If y1 + x > 550 Then y1 = Int(Rnd() * x)
                        If y2 + x > 550 Then y2 = Int(Rnd() * x)
                        formGraphics1.DrawEllipse(myPen1, y1, y2, x, x)
                        myPen1.Dispose()
                        formGraphics1.Dispose()
                    Next
                Case "並排橢圓"
                    If Val(ComboBox2.Text) > 500 Then
                        MsgBox("數值太大")
                        Exit Select
                    End If
                    Dim x1 As Integer = Int(Rnd() * 550)
                    Dim x2 As Integer = Int(Rnd() * 550)
                    Dim g As Integer = 0
                    Dim s As Byte = Int(Rnd() * 100)
                    If s Mod 2 = 0 Then
                        For i = 1 To Val(ComboBox2.Text)
                            Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                            Dim formGraphics1 As System.Drawing.Graphics
                            formGraphics1 = Me.PictureBox1.CreateGraphics()
                            g = g + Val(ComboBox3.Text)
                            formGraphics1.DrawEllipse(myPen1, 100, g, x1, x2)
                            myPen1.Dispose()
                            formGraphics1.Dispose()
                        Next
                    Else
                        For i = 1 To Val(ComboBox2.Text)
                            Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                            Dim formGraphics1 As System.Drawing.Graphics
                            g = g + Val(ComboBox3.Text)
                            formGraphics1 = Me.PictureBox1.CreateGraphics()
                            formGraphics1.DrawEllipse(myPen1, g, 100, x1, x2)
                            myPen1.Dispose()
                            formGraphics1.Dispose()
                        Next
                    End If
                Case "並排圓"
                    If Val(ComboBox2.Text) > 500 Then
                        MsgBox("數值太大")
                        Exit Select
                    End If
                    Dim x As Integer = Int(Rnd() * 550)
                    Dim g As Integer = 0
                    Dim s As Byte = Int(Rnd() * 100)
                    If s Mod 2 = 0 Then
                        For i = 1 To Val(ComboBox2.Text)
                            Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                            Dim formGraphics1 As System.Drawing.Graphics
                            formGraphics1 = Me.PictureBox1.CreateGraphics()
                            g = g + Val(ComboBox3.Text)
                            formGraphics1.DrawEllipse(myPen1, 100, g, x, x)
                            myPen1.Dispose()
                            formGraphics1.Dispose()
                        Next
                    Else
                        For i = 1 To Val(ComboBox2.Text)
                            Dim myPen1 As New System.Drawing.Pen(System.Drawing.Color.FromArgb(255, Int(Rnd() * 255), Int(Rnd() * 255), Int(Rnd() * 255)))
                            Dim formGraphics1 As System.Drawing.Graphics
                            g = g + Val(ComboBox3.Text)
                            formGraphics1 = Me.PictureBox1.CreateGraphics()
                            formGraphics1.DrawEllipse(myPen1, g, 100, x, x)
                            myPen1.Dispose()
                            formGraphics1.Dispose()
                        Next
                    End If
                Case Else
                    MsgBox("輸入錯誤!")
            End Select
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.Text = "並排圓" Or ComboBox1.Text = "並排橢圓" Then
                Label3.Visible = True
                ComboBox3.Visible = True
                Label4.Visible = True
            Else
                Label3.Visible = False
                ComboBox3.Visible = False
                Label4.Visible = False
            End If
        End Sub
    End Class
    2009年9月13日 上午 07:38

解答

  • 可以的話,在記憶體繪圖,畫完在指給 PictureBox 會比較快。

    把:
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()

                            formGraphics1.Dispose()

    移道迴圈外,會比較快。


    論壇是網友平等互助 保證解答請至 微軟技術支援服務
    • 已標示為解答 Sally150 2009年9月15日 下午 01:52
    2009年9月13日 上午 10:40
  • 小弟認為
    1.可以先算好數量再跑迴圈
    For i = 1 To Val(ComboBox2.Text)
        ......................
    Next

    改成

    dim count as integer = cint(ComboBox2.Text)
    For i as integer = 1 To count
        ......................
    Next



    2.在windows下integer運算比byte快,還有別讓編譯器去幫你轉型
    Dim s As Byte = Int(Rnd() * 100)
    改成
    Dim s As integer = Int(Rnd() * 100)


    • 已標示為解答 Sally150 2009年9月15日 下午 01:51
    2009年9月14日 上午 02:06

所有回覆

  • 可以的話,在記憶體繪圖,畫完在指給 PictureBox 會比較快。

    把:
                        Dim formGraphics1 As System.Drawing.Graphics
                        formGraphics1 = Me.PictureBox1.CreateGraphics()

                            formGraphics1.Dispose()

    移道迴圈外,會比較快。


    論壇是網友平等互助 保證解答請至 微軟技術支援服務
    • 已標示為解答 Sally150 2009年9月15日 下午 01:52
    2009年9月13日 上午 10:40
  • 小弟認為
    1.可以先算好數量再跑迴圈
    For i = 1 To Val(ComboBox2.Text)
        ......................
    Next

    改成

    dim count as integer = cint(ComboBox2.Text)
    For i as integer = 1 To count
        ......................
    Next



    2.在windows下integer運算比byte快,還有別讓編譯器去幫你轉型
    Dim s As Byte = Int(Rnd() * 100)
    改成
    Dim s As integer = Int(Rnd() * 100)


    • 已標示為解答 Sally150 2009年9月15日 下午 01:51
    2009年9月14日 上午 02:06