dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Find macro bottleneck

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,028

    Resolved [RESOLVED] Find macro bottleneck

    First let me say that I know how to time code and in fact I've written an article on the subject.

    My problem is that I have a situation where a sheet in an Excel workbook contains about 3500 rows and for each of about 1100 of those rows I need to perform 7 macros. The whole process takes about 2 minutes to run whereas a very similar process takes only 15 seconds, and I'd like to determine the total time spent in each of those seven macros to try to determine if one or more of the macros is a bottleneck. Is there an existing free or paid tool that will do that or can someone come up with a DIY scheme to do it?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,819

    Re: Find macro bottleneck

    not hard to use a timer of some sort to record the time each macro takes, especially if the 7 macros are called consecutively
    depending on the required accuracy you could try timer, gettickcont or queryperformancecounter
    i would assume that timer would be accurate enough, for what you need to do, you could sum the times for each macro into an array, then save the results to file or excel sheet for later comparisons
    even possible to compare the times spent for each row processed if required
    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
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,500

    Re: Find macro bottleneck

    Agree with Pete, but with slight differences.

    I'd use Gettickcount to log entering a macro, and the same when exiting.
    Result into a 2D-Array (or maybe a separate sheet)
    Kind of like

    Row - Macro1Enter, Macro1Exit, Macro1TimeLen, Macro2Enter, Macro2Exit, Macro2TimeLen etc...... (TimeLen being Exit minus Enter)

    It should give you the hint which one is causing trouble.
    It basically doesn't matter if you're using milliseconds, ticks, [whatever] since you just need the representation of the needed time.
    The higher the value of TimeLen, the more time you need for it, independent of the used unit
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  4. #4

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,028

    Re: Find macro bottleneck

    Thank you both for your help. I started out by adding a new sheet to the workbook and writing the elapsed time (as timed using GetTickCount) of each of the 7 macros to its own column with the intent to average each column. Part way through that process however I found the real problem and that was that automatic calculation was allowed to continue in the long-running process and when I changed that to manual the problem went away.

  5. #5
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    570

    Re: [RESOLVED] Find macro bottleneck

    Here's a class I've been using over the years. It uses vb's Timer function and keeps track of timers by name. Timers accumulate if called multiple times.

    Usage:
    Code:
    Sub TimeStuff()
        Dim sw as New CStopwatch
        Dim i as Long
    
        For i = 1 to 100
            sw.StartTimer "Process 1"
            'Perform task
            sw.StopTimer "Process 1"
    
            sw.StartTimer "Process 2"
            'Perform task
            sw.StopTimer "Process 2"
        Next
    
        sw.Report 'Outputs to immediate window
    End Sub
    Class CStopwatch
    Code:
    Option Explicit
    
    Private Type tTimer
        Name As String
        Start As Single
        Stop As Single
        RunTime As Single
    End Type
    
    Private mTimerCount As Byte
    Private mTimers(255) As tTimer
    
    Private Sub Class_Terminate()
        Erase mTimers
    End Sub
    
    Public Sub StartTimer(Name As String)
        Dim arrPos As Byte
        Dim found As Boolean
        
        found = FindTimer(Name, arrPos)
        
        If Not found Then mTimers(arrPos).Name = Name
        mTimers(arrPos).Stop = 0
        mTimers(arrPos).Start = Timer
    End Sub
    
    Public Sub StopTimer(Name As String)
        Dim arrPos As Byte
        Dim found As Boolean
        
        found = FindTimer(Name, arrPos, False)
        
        If Not found Then
            Debug.Print "Trying to stop timer " & Name & " but no such timer exists."
            Exit Sub
        End If
        
        mTimers(arrPos).Stop = Timer
        mTimers(arrPos).RunTime = mTimers(arrPos).RunTime + mTimers(arrPos).Stop - mTimers(arrPos).Start
    End Sub
    
    Public Sub Report()
        Dim i As Byte
        
        If mTimerCount = 0 Then
            Debug.Print "Nothing to report."
        End If
    
        For i = 1 To mTimerCount
            If mTimers(i).Stop = 0 Then StopTimer mTimers(i).Name
            Debug.Print mTimers(i).Name & ": " & Format(mTimers(i).RunTime, "#,##0.00s")
        Next
    End Sub
    
    Private Function FindTimer(Name As String, ByRef ArrayPos As Byte, Optional IncreasePosition As Boolean = True) As Boolean
        Dim found As Boolean
        
        If mTimerCount = 0 Then
            mTimerCount = 1
            ArrayPos = 1
        Else
            For ArrayPos = 1 To mTimerCount
                If mTimers(ArrayPos).Name = Name Then
                    found = True
                    Exit For
                End If
            Next
            If Not found Then
                ArrayPos = mTimerCount + 1
                If IncreasePosition Then mTimerCount = ArrayPos
            End If
        End If
        
        FindTimer = found
    End Function

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width