Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1412to1416
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Code beschleunigen

Code beschleunigen
19.03.2015 11:20:10
Robert
Liebe VBA Pro´s,
Ich habe eine kleine applikation programmiert die leider nur sehr sehr langsam läuft - >30 je nachdem wieviele simulationsschritte und elemente.
ich habe gehöfft von euch einige tips zu bekommen wie die applikation 2optimiert werden könnte.
beste grüße,
Robert
hier der code

Public Sub SimulateIPCCSystem()
SimulationRuns = 10
Call AnalyticalCalc
Call ReadPrimaryInputParameters             'primary input parameters
Call CalculateSecondaryInputParameters      'secondary input parameters calculation
Progress.Show 0
Call OptimizeCode_Begin
For s = 1 To SimulationRuns
Call Progress1
'Progress.Show 0
Call initializesimulation                   'set start conditions
'--------------------------------------------------Beginning of Simulation---------------------- _
_
_
Do Until Clock = (AvailableShiftsperYear * ShiftLenght + 1) - ((OperatingDelays2 * ( _
ShiftDuration - PlannedDelays) * AvailableShiftsperYear) * 3600)
If Clock = NextShiftTime Then
Call PrintResults
'Call Progress1
'Call TruckavailableperShift
Call Event10_NextShift
shift = shift + 1
End If
If Clock = LoaderWorkTime Then
Call Event6_NextRepairtimeLoader
LoaderStatusOperational = "InRepair"
End If
If Clock = LoaderRepairTime Then
Call Event5_NextWorkTimeLoader
LoaderStatusOperational = "Working"
End If
If Clock = IPCCWorkTime Then
Call Event8_NextIPCCMeanRepairTime
CrusherStatusOperational = "InRepair"
End If
If Clock = IPCCRepairTime Then
Call Event7_NextWorkTimeIPCC
CrusherStatusOperational = "Working"
End If
If TotalTonnage >= MaxDumpVolume * (1 + TrackShiftCounter) Then
Call Event9_NextTrackshiftTime
IPCCStatus = "Trackshifting"
TrackShiftCounter = TrackShiftCounter + 1
End If
If Clock = NextTrackshiftTime Then
IPCCStatus = "NotTrackshifting"
End If
If TotalTonnage >= MaxBlastVolume * (1 + BlastCounter) Then
Call Event11_NextBlastDelay
BlastStatus = "Blasting"
BlastCounter = BlastCounter + 1
End If
If Clock = NextBlastDelayTime Then
BlastStatus = "NotBlasting"
End If
For T = 1 To TruckNumber
If Clock = TruckTraveltimeUnloaded(T) And Truckstatus(T) = "TravelUnloaded" Then
Truckstatus(T) = "InLoaderQueue"
QueueLoader = QueueLoader + 1
End If
If Truckstatus(T) = "InLoaderQueue" And LoaderStatus = "NotLoading" And  _
LoaderStatusOperational = "Working" And BlastStatus = "NotBlasting" And IPCCStatus = " _
NotTrackshifting" And UsedLoaderSpots = 0 Then
Truckstatus(T) = "Spotting"
If Truckexchange = "yes" Then
TruckSpotTime(T) = SpotTimeAtLoader + TruckExchangeTime + Clock
Else
If Clock - marker  10 Then
i = i
End If
Else
TruckSpotTime(T) = SpotTimeAtLoader + Clock
End If
End If
QueueLoader = QueueLoader - 1
Randomize
FirstBucketTime = WorksheetFunction.Round(WorksheetFunction.GammaInv(Rnd(), alpha, beta) _
_
_
, 0) + Clock - LoaderDumpTime
TotalTruckLoadingTime = TotalTruckLoadingTime + FirstBucketTime
LoaderStatus = "Loading"
UsedLoaderSpots = 1
End If
If Clock = FirstBucketTime Then
LoaderStatus = "NotLoading"
End If
If Clock = TruckSpotTime(T) And Truckstatus(T) = "Spotting" And LoaderStatus = " _
NotLoading" And LoaderStatusOperational = "Working" And BlastStatus = "NotBlasting" And  _
IPCCStatus = "NotTrackshifting" Then
Truckstatus(T) = "GettingLoaded"
LoaderStatus = "Loading"
Call Event1_NextLoadingTime
TruckloadingTime(T) = EventTime1
TruckPayload(T) = EventTime1a
TotalTonnageLoader = TotalTonnageLoader + EventTime1a
UsedLoaderSpots = 0
End If
If QueueLoader > 0 And LoaderStatusOperational = "Working" And BlastStatus = " _
NotBlasting" And IPCCStatus = "NotTrackshifting" Then
Truckexchange = "yes"
Else
Truckexchange = "no"
marker = Clock
End If
If Clock = TruckloadingTime(T) And Truckstatus(T) = "GettingLoaded" Then
Truckstatus(T) = "TravelLoaded"
LoaderStatus = "NotLoading"
Call Event2_NextTravelTimeLoaded
TruckTraveltimeLoaded(T) = EventTime2
End If
If Clock = TruckTraveltimeLoaded(T) And Truckstatus(T) = "TravelLoaded" Then
Truckstatus(T) = "InCrusherQueue"
QueueCrusher = QueueCrusher + 1
CrusherQueuePosition(QueueCrusher) = T
End If
If Truckstatus(T) = "InCrusherQueue" And HopperVolume + TruckPayload(T) = 50 And CrusherStatusOperational = "Working" And IPCCStatus = " _
NotTrackshifting" And BlastStatus = "NotBlasting" Then
CrusherStatus = "Crushing"
Else
CrusherStatus = "NotCrushing"
End If
If CrusherStatus = "NotCrushing" And CrusherStatusOperational = "Working" And  _
IPCCStatus = "NotTrackshifting" And BlastStatus = "NotBlasting" Then
IdleTimeCrusher = IdleTimeCrusher + 1
End If
If CrusherStatus = "Crushing" Then
HopperVolume = HopperVolume - (CrusherCapacity / 60 / 60)
TotalTonnage = TotalTonnage + (CrusherCapacity / 60 / 60)
EffectiveOperatingTimeIPCC = EffectiveOperatingTimeIPCC + 1
End If
If CrusherStatusOperational = "Working" And IPCCStatus = "NotTrackshifting" And  _
BlastStatus = "NotBlasting" Then
TotalWorkTimeIPCC = TotalWorkTimeIPCC + 1
End If
If CrusherStatusOperational = "InRepair" And IPCCStatus = "NotTrackshifting" And  _
BlastStatus = "NotBlasting" Then
TotalIPCCRepairTime = TotalIPCCRepairTime + 1
End If
'If LoaderStatusOperational = "Working" And IPCCStatus = "NotTrackshifting" And  _
BlastStatus = "NotBlasting" Then
'TotalWorktimeLoader = TotalWorktimeLoader + 1
'End If
If LoaderStatusOperational = "InRepair" And BlastStatus = "NotBlasting" And  _
IPCCStatus = "NotTrackshifting" Then
TotalRepairTimeloader = TotalRepairTimeloader + 1
End If
If LoaderStatus = "NotLoading" And LoaderStatusOperational = "Working" And  _
BlastStatus = "NotBlasting" And IPCCStatus = "NotTrackshifting" Then
IdleTimeLoader = IdleTimeLoader + 1
End If
If LoaderStatus = "Loading" And LoaderStatusOperational = "Working" And BlastStatus  _
_
_
= "NotBlasting" And IPCCStatus = "NotTrackshifting" Then   '
EffectiveOperatingTimeLoader = EffectiveOperatingTimeLoader + 1
End If
TotalTime = TotalTime + 1
If (EffectiveOperatingTimeLoader + TotalRepairTimeloader + IdleTimeLoader +  _
TotalBlastingTime + TotalTrackShiftTime)  TotalTime Then
'Call PrintResults
End If
If EffectiveOperatingTimeIPCC + IdleTimeCrusher + TotalIPCCRepairTime +  _
TotalBlastingTime + TotalTrackShiftTime  TotalTime Then
'Call PrintResults
End If
counter = counter + 1
Clock = Clock + 1
Loop
Call OptimizeCode_End
Call PrintResults
Call PrintTUMIPCC
'Unload Progress
Next s
Unload Progress
Call PrintFinalResults
End Sub

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Code beschleunigen
19.03.2015 11:41:53
nike
Hi,
coole Variablenbenennung ;-)
Ich würde mal die Üblichen "Beschleuniger" versuchen:
'am Anfang die automatische Berechnung ausknipsen
Application.Calculation = xlCalculationManual
'dann den Bildschirm ausknipsen
'Application.ScreenUpdating = False
'und wieder an
'Application.ScreenUpdating = True
'und am Ende die Berechnung wieder an
Application.Calculation = xlCalc
Wenn Du den Fortschritt anzeigen willst/musst dann kurz davor den Bildschrim wieder auffrischen lassen
und danach wieder aus knipsen...
Vielleicht würde es auch gehen gewisse Werte nicht immer neu berechnen zu lassen sondern ein mal in eine Variable zu schreiben und dann dagegen prüfen - hängt aber von den Veränderungen in der Logik ab...
Den Code im Detail zu analysieren übersteigt glaub ich etwas den Rahmen eines kostenlosen Formus ;-)
CU
Nike

Anzeige
AW: Code beschleunigen
19.03.2015 12:07:53
Daniel
Hi
um hier die Geschwindigkeit zu optimieren, müsste man die ganze Datei kennen.
zunächsteinmal solltest du versuchen, die "Zeitfresser" zu finden und die könnten auch in deinen zahlreichen Untermakros stecken.
Oder in Formeln in der Tabelle, die öfters neu berechnet werden.
ohne die Hintergründe deiner Aufgabe und die vollständige Datei zu kennen, sehe ich in dem gezeigten Code folgende Optimierungsansätze (wobei das eher kleinigkeiten sind)
1. Variablen deklarieren
undeklarierte Variabeln sind immer vom Typ Variant und dies ist in der Verarbeiung der langsamste Typ.
(arbeitest du mit Option Explicit
2. keine ANDs in IF-THEN
sondern wenn mehere AND-Bediungugen vorhanden sind, diese über ineinander geschachtelte IF-Blöcke darstellen.
If A Then
if B Then
Mach was
end If
End if

ist etwas schneller als
if A AND B Then
Mach was
End if

weil die AND-Verknüpfung immer vollständig durchgerechnet wird.
dh im zweiten Fall wird die Bedingung B immer überprüft, während in den geschachtelten IF-Blöcken der Fall B nur dann überprüft wird, wenn A erfüllt ist.
Das eine sinnvolle Reihenfolge der Prüfungen für den Erfolg der Optimierung entscheidend sein kann, liegt auf der Hand.
Gruß Daniel
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige