Programm wird langsamer

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von _PASCAL_.

    Programm wird langsamer

    Hallo,

    ich habe ein Programm gebastelt welche einige berechnungen durchführt.

    Jetzt ist mir aufgefallen, dass die Berechnungsschleife nach hinten raus immer langsamer wird, trotz gleich großem berechnungsaufwand.

    Da ist mir der einfall gekommen dass vll der Speicher ein bisschen zugemüllt wird.

    Jetzt müsste ich wissen woran es liegen könnte ... Wenn ich mit List(Of T) arbeite und mir immer wieder eine neue mit temp = New List(Of T) erstelle ... was passiert dann mit der alten ... sollte ich da vorher temp.Dispose() schreiben oder ist das überflüssig ?

    bei temp.Clear() denke ich mal dass der Speicher des aktuellen Pointers wieder frei ist und ich das neu beschreiben kann.
    Nein die schleife soll ja fertig laufen ... es ist nicht so dass die schleife nicht beendet wird.

    mir fällt nur auf dass die Berechnung zum Ende immer länger dauert, dabei sind die zu berechnenden Teilstücke nicht größer als die am Anfang.

    Da ich jetzt noch eine ungefähre Endzeit anzeigen will macht mir das zu schaffen. Ich Stoppe immer die Zeit wie lange eine Berechnung dauert und nehme davon den Durchschnitt ... diesen multipliziere ich dann mit den noch bevorstehenden Teilstücken ...

    das funktioniert aber nicht richtig, da der Durchschnitt zum ende ja ansteigt da die Berechnung erheblich länger dauert.
    also das ist relativ viel ... hier mal die eigentliche funktion welche vom backgroundworker ausgeführt wird und 2 funktionen die aufgerufen werden.

    speziell die "analyse" funktion wird recht langsam mit der zeit ...

    über generelle rückmeldung bezüglich meines Programmierstiles würde ich mich auch freuen ^^

    VB.NET-Quellcode

    1. Public Sub calculate()
    2. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Exit Sub
    3. active_mesGroup.data.Clear()
    4. For Each File In calc_all_files
    5. calc.ReportProgress(100 * (calc_all_files.IndexOf(File) / calc_all_files.Count), CalcState.Fraction)
    6. Dim fractions As List(Of dataFraction) = calc_fractions(File)
    7. If Not IsNothing(fractions) Then active_mesGroup.data.AddRange(fractions)
    8. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Exit Sub
    9. Next
    10. If Not active_mesGroup.data.Count > 0 Then args.Cancel = True : args.Result = "Keine Daten zum berechnen gefunden" & vbNewLine & "Bitte überprüfen Sie ihre gewählten Einstellungen"
    11. calc.ReportProgress(0, CalcState.Marker)
    12. For Each mes In active_mesGroup.messurements
    13. Select Case mes.type
    14. Case MessType.FLANKE
    15. calc_flanke(mes)
    16. Case MessType.MIN
    17. calc_max(mes)
    18. Case MessType.MAX
    19. calc_min(mes)
    20. Case MessType.KONSTANT
    21. calc_konst(mes)
    22. End Select
    23. Next
    24. calc.ReportProgress(0, CalcState.AnalyseInit)
    25. If active_mesGroup.fullmes Then
    26. Dim del_frac As New List(Of dataFraction)
    27. For Each frac In active_mesGroup.data
    28. If Not frac.markers.Count = active_mesGroup.messurements.Count Then
    29. del_frac.Add(frac)
    30. End If
    31. Next
    32. For Each frac In del_frac
    33. active_mesGroup.data.Remove(frac)
    34. Next
    35. End If
    36. If Not active_mesGroup.data.Count > 0 Then args.Cancel = True : args.Result = "Keine Daten zum berechnen gefunden" & vbNewLine & "Bitte überprüfen Sie ihre gewählten Einstellungen"
    37. analyse()
    38. End Sub


    VB.NET-Quellcode

    1. Public Function calc_fractions(ByVal File As source_File, Optional ByVal index As Integer = 0) As List(Of dataFraction)
    2. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    3. If File.channels.ContainsKey(active_mesGroup.data_channel.name) Then
    4. Dim channel As channel = File.channels.Item(active_mesGroup.data_channel.name)
    5. Dim Temp As SpecialList(Of XYPointPair) = Nothing
    6. If File.Type = FileType.TMS Then
    7. Temp = tms_read.read_Signal(File.Attributes.FullName, channel.group.index, channel.column)
    8. Else
    9. Temp = asc_read.read_Signal(File.Attributes.FullName, channel.group.index, channel.column)
    10. End If
    11. Dim fraction As New dataFraction
    12. Dim active_fraction As Long = -1
    13. Dim frac As List(Of dataFraction)
    14. If active_mesGroup.fraction Then
    15. frac = New List(Of dataFraction)
    16. If Temp(0).Y > 0 Then
    17. fraction.group = channel.group
    18. frac.Add(fraction)
    19. active_fraction += 1
    20. End If
    21. For i As Integer = 0 To Temp.Count - 2
    22. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    23. If Temp(i).Y < 1 And Temp(i + 1).Y >= 1 Then
    24. frac.Add(New dataFraction(channel.group))
    25. active_fraction += 1
    26. frac(active_fraction).DataPairList.Add(New XYPointPair(Temp(i)))
    27. End If
    28. If (active_fraction) < frac.Count AndAlso (active_fraction) >= 0 Then
    29. If i > 1 Then
    30. If Not Temp(i - 1).Y < 1 Then
    31. frac(active_fraction).DataPairList.Add(New XYPointPair(Temp(i)))
    32. End If
    33. End If
    34. End If
    35. Next
    36. Else
    37. frac = New List(Of dataFraction)
    38. frac.Add(New dataFraction(Temp, channel.group))
    39. End If
    40. Dim del_fractions As New List(Of dataFraction)
    41. If active_mesGroup.limit > 0 Then
    42. For Each fraction In frac
    43. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    44. If fraction.DataPairList.Count > 0 Then
    45. Dim y As Double = fraction.DataPairList.get_Y.Max
    46. If y < active_mesGroup.limit Then
    47. del_fractions.Add(fraction)
    48. End If
    49. End If
    50. Next
    51. End If
    52. For Each fraction In frac
    53. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    54. If active_mesGroup.data_channel.att_name Like "geschwindigkeit" Then
    55. integrate(fraction)
    56. End If
    57. Next
    58. If active_mesGroup.way_max > 0 Then
    59. For Each fraction In frac
    60. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    61. If fraction.DataPairList.Count > 0 Then
    62. If fraction.way.Last.Y < active_mesGroup.way_min Or fraction.way.Last.Y > active_mesGroup.way_max Then
    63. If Not del_fractions.Contains(fraction) Then
    64. del_fractions.Add(fraction)
    65. End If
    66. End If
    67. End If
    68. Next
    69. End If
    70. For Each fraction In frac
    71. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    72. If fraction.DataPairList.Count < 10 Then
    73. If Not del_fractions.Contains(fraction) Then
    74. del_fractions.Add(fraction)
    75. End If
    76. End If
    77. Next
    78. For Each databra In del_fractions
    79. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Return Nothing
    80. frac.Remove(databra)
    81. Next
    82. Return frac
    83. End If
    84. Return Nothing
    85. End Function


    VB.NET-Quellcode

    1. Private Sub analyse()
    2. Dim temp As XYPointPair
    3. Dim index As Integer
    4. Dim result As String = ""
    5. Dim result2 As String = ""
    6. For Each fraction As dataFraction In active_mesGroup.data
    7. active_fraction = active_mesGroup.data.IndexOf(fraction)
    8. For Each mes As messurement In active_mesGroup.messurements
    9. For Each ca As calculate In mes.calculation
    10. For Each mark As marker In fraction.markers
    11. calc.ReportProgress(100 * ((active_fraction + 1) / active_mesGroup.data.Count), CalcState.Analyse)
    12. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Exit Sub
    13. If fraction.group.Parent.channels.ContainsKey((ca.column(0))) Then
    14. If fraction.group.Parent.Type = FileType.TMS Then
    15. temp = tms_read.read_Point(fraction.group.Parent, fraction.group.Parent.channels.Item(ca.column(0)), mark.MessPoint.X)
    16. Else
    17. temp = asc_read.read_Point(fraction.group.Parent, fraction.group.Parent.channels.Item(ca.column(0)), mark.MessPoint.X)
    18. End If
    19. index = temp.index
    20. result = temp.Y
    21. Else
    22. Try
    23. result = (CDbl(ca.column(0))).ToString
    24. index = -1
    25. Catch exx As Exception
    26. result = ""
    27. End Try
    28. End If
    29. Dim sc As New MSScriptControl.ScriptControl()
    30. sc.Language = "VBScript"
    31. For i As Integer = 1 To ca.column.Count - 1
    32. If calc.CancellationPending Then args.Cancel = True : args.Result = "abbruch" : Exit Sub
    33. If fraction.group.Parent.channels.ContainsKey((ca.column(i))) Then
    34. If fraction.group.Parent.Type = FileType.TMS Then
    35. temp = tms_read.read_Point(fraction.group.Parent, fraction.group.Parent.channels.Item(ca.column(i)), mark.MessPoint.X)
    36. Else
    37. temp = asc_read.read_Point(fraction.group.Parent, fraction.group.Parent.channels.Item(ca.column(i)), mark.MessPoint.X)
    38. End If
    39. result2 = temp.Y.ToString
    40. Else
    41. Try
    42. result2 = (CDbl(ca.column(i))).ToString
    43. Catch exx As Exception
    44. result2 = ""
    45. End Try
    46. End If
    47. Dim expr As String = result.ToString & ca.operators(i - 1) & result2
    48. expr = Replace(expr, ",", ".")
    49. result = sc.Eval(expr)
    50. Next i
    51. ca.result.Add(New result(Math.Round(CDbl(result), 3), fraction.group, index, active_mesGroup.data.IndexOf(fraction)))
    52. Next mark
    53. Next ca
    54. Next mes
    55. Next fraction
    56. End Sub

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „_PASCAL_“ ()

    Im Sub Analyse(), hast du 5 For Schleifen geschachtelt (Und überhaupt (auch in anderen Subs) verdammt viele)

    Ich weiß nicht wie viele Durchgänge die Schleifen haben, aber das dürfte der Grund sein, wieso dein Programm so langsam ist.
    ich weis dass es einige schleifen sind ... aber es geht nunmal nicht anders ... ich muss alles abarbeiten was in den listen steht.

    es gibt so um die 200 Fractions

    in jeder Fraction befinden sich ungefähr 500-600 einträge

    und in diesen befinden sich zum schluss 0-3 marker welche alle berücksichtigt werden müssen

    zu jedem marker wird er noch ein MessPunkt aus einer tms(TELLER MessDatei) oder asc(TXT File) benötigt was auch en bissl dauert
    die read funktionen von den asc bzw tms dateien müssen sich mit bis zu 50000 einträgen herumschlagen wobei am ende nur ein punkt benötigt wird

    wenn ihr mir eine schnellere lösung bzw ein anderen Grundgerüst empfehlen könnt könnt ich das vll umsetzen aber ich seh keine andere möglichkeit

    VB.NET-Quellcode

    1. Public Function read_Point(ByVal File As source_File, ByVal channel As channel, ByVal MessPoint As Double) As XYPointPair
    2. Dim temp As New SpecialList(Of XYPointPair)
    3. Dim SampleCount As UInt64
    4. If (TilSetString(FileName, File.Attributes.FullName)) Then
    5. Data_tms = TilGetProperty(Root, tilData)
    6. Meas = TilGetProperty(Data_tms, tilDataMeas)
    7. Meas0 = TilGetVectorItem(Meas, 0)
    8. Groups = TilGetProperty(Meas0, tilDataMeasGroup)
    9. nGroups = TilGetVectorSize(Groups)
    10. Dim Group As IntPtr
    11. Dim Signals As IntPtr
    12. Dim nSignals As Integer
    13. Dim SampleRate As Double
    14. Dim Prop As IntPtr
    15. Dim Info As IntPtr
    16. Dim Time As IntPtr
    17. Dim Time64 As IntPtr
    18. Dim SignalData As IntPtr
    19. Dim View As IntPtr
    20. ' Initialize property values
    21. SampleCount = 0
    22. SampleRate = 0
    23. Group = TilGetVectorItem(Groups, channel.group.index)
    24. Prop = TilGetProperty(Group, tilDataMeasGroupSampleRate)
    25. TilGetUInt64(Prop, SampleRate)
    26. Prop = TilGetProperty(Group, tilDataMeasGroupSampleCount)
    27. TilGetUInt64(Prop, SampleCount)
    28. Info = TilGetProperty(Group, tilDataMeasGroupInfo)
    29. Dim f As IntPtr = TilGetProperty(Group, tilDataMeasGroup)
    30. Dim test As String = GetTilString(f)
    31. Time = TilGetProperty(Info, tilDataMeasGroupInfoTime)
    32. Time64 = TilGetProperty(Time, tilDataMeasGroupInfoTimeHWReadOut)
    33. Signals = TilGetProperty(Group, tilDataMeasGroupSignal)
    34. nSignals = TilGetVectorSize(Signals)
    35. Dim Signal As IntPtr
    36. Dim Comm As IntPtr
    37. Signals = TilGetProperty(Group, tilDataMeasGroupSignal)
    38. nSignals = TilGetVectorSize(Signals)
    39. Samples = New Double(1)() {}
    40. Samples(0) = New Double(SampleCount - 1) {}
    41. Signal = TilGetVectorItem(Signals, 0)
    42. Comm = TilGetProperty(Signal, tilDataMeasGroupSignalComm)
    43. SignalData = TilGetProperty(Signal, tilDataMeasGroupSignalData)
    44. View = TilGetProperty(Signal, tilDataMeasGroupSignalView)
    45. Prop = TilGetProperty(SignalData, tilDataMeasGroupSignalDataIterator)
    46. If (Prop) Then
    47. ' Note, the iterator property may not exist for data type dt_f80 on Windows x64 platforms
    48. TilGetNextDoubleIteratorItems(Prop, Samples(0), SampleCount)
    49. temp.set_X(Samples(0))
    50. End If
    51. Samples(1) = New Double(SampleCount - 1) {}
    52. Signal = TilGetVectorItem(Signals, channel.column)
    53. Comm = TilGetProperty(Signal, tilDataMeasGroupSignalComm)
    54. SignalData = TilGetProperty(Signal, tilDataMeasGroupSignalData)
    55. View = TilGetProperty(Signal, tilDataMeasGroupSignalView)
    56. Prop = TilGetProperty(SignalData, tilDataMeasGroupSignalDataIterator)
    57. If (Prop) Then
    58. ' Note, the iterator property may not exist for data type dt_f80 on Windows x64 platforms
    59. TilGetNextDoubleIteratorItems(Prop, Samples(1), SampleCount)
    60. temp.set_Y(Samples(1))
    61. End If
    62. End If
    63. Return temp.get_XY(MessPoint)
    64. End Function


    VB.NET-Quellcode

    1. Public Function read_Point(ByVal File As source_File, ByVal channel As channel, ByVal MessPoint As Double) As XYPointPair
    2. Dim x As Double
    3. Dim y As Double
    4. Dim reader_stream As New StreamReader(File.get_Path)
    5. reader_stream.ReadLine()
    6. reader_stream.ReadLine()
    7. Dim signal_index As Short = File.channels(channel.name).column
    8. Dim buffer As String()
    9. Dim result As New SpecialList(Of XYPointPair)
    10. Do While Not reader_stream.EndOfStream
    11. buffer = reader_stream.ReadLine.Split(vbTab)
    12. x = CDbl(buffer(0))
    13. y = CDbl(buffer(signal_index))
    14. result.Add(New XYPointPair(x, y))
    15. Loop
    16. Return result.get_XY(MessPoint)
    17. End Function