Results 1 to 4 of 4

Thread: slow loop! any advice??

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    2

    slow loop! any advice??

    I have a loop which currently runs 96*19*10 = 18,240 times and it's taking a long time (about 5 minutes). Is this to be expected? Anyone have any advice on how to increase efficiency? Thanks!!

    Code:
    Sub simulations()
    
    'set timer
    Dim StartTime As Double
    Dim EndTime As Double
    
    StartTime = Timer
    
    Application.ScreenUpdating = False
        'Select toggle sheet to start
    
    Dim iterations As Integer
    Dim days As Integer
    Dim startdate As Date
    Dim securities As Integer
    
    Dim i As Long
    Dim j As Integer
    Dim d As Long
    
    Dim swap As String
    Dim rows As Integer
    Dim cols As Integer
    
    
    iterations = Range("iterations")
    days = Range("dates").rows.Count - 1
    startdate = DateSerial(2008, 5, 30)
    n = 1
    securities = Range("swaps").Columns.Count
    
    ReDim Output(1 To iterations) As Long
    ReDim Results(1 To securities, 1 To days) As Long
    
    For Each security In Range("swaps")
    
        swap = security
        Range("swap") = security
    
        
        'loop through valuation dates
        For d = 1 To days
        
            Range("ValuationDate") = Range("base").Offset(Application.Match(CLng(startdate), Range("dates")) - 2 + d, 0)
        
                'loop through iterations
                For i = 1 To iterations
            
                    Range("toggle") = i
                
                    Output(i) = Application.WorksheetFunction.Sum(Range("results"))
                  
                Next
            
            On Error Resume Next
            Results(n, d) = Application.WorksheetFunction.Average(Output) / Range("spotvaluationdate")
            Erase Output
            ReDim Output(1 To iterations) As Long
    
        Next
        
        'increment swap number
        n = n + 1
    
    Next
    
        'paste results
        rows = Application.Match(CLng(startdate), Range("dates"), 0) - 1
        Range(Range("base").Offset(rows, 1), _
              Range("base").Offset(rows + d - 2, securities)) = WorksheetFunction.Transpose(Results)
    
    
    
    'display run time
    EndTime = Timer - StartTime
    MsgBox EndTime
    
    
    End Sub
    Last edited by si_the_geek; Aug 17th, 2012 at 11:35 AM. Reason: added Code tags

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    Re: slow loop! any advice??

    please use code tags when posting code

    how are you calling this procedure? is it called from some other procedure?

    what is output used for? i can see values are assigned to it, but then it is redimmed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    2

    Re: slow loop! any advice??

    Sorry I'm new to the forum I'm sure I made all sorts of posting faux pas.

    This procedure is not being called by another procedure. It is run on its own and the results (Result array) is pasted into the worksheet.

    The output array is a temporary array used to store the results of each iteration, and then the average value of the values in Output is put into the Results array.

    Thanks!

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    Re: slow loop! any advice??

    then the average value of the values in Output is put into the Results array.
    i missed that bit

    do you have any code in worksheet change (or other) event?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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