+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,006

    Speed Testing Workbooks

    The microtimer routine was taken from the MSDN Site http://msdn.microsoft.com/en-us/library/aa730921.aspx in an article by Charles Williams

    I use this code a lot when testing workbooks to time functions, hope it is of some use to you!


    it provides a macro

    TimeSheet

    Which will produce a new worksheet with timings of all formulas found in the active sheet, sorted slowest first!

    and 2 functions

    TimeCell(Cell,iterations)

    This will return the time it took to calculate that formula note the more iterations the more acurate!

    FormulaIs(Cell)

    Just so I can see the formula!


    PHP Code:
    Option Explicit
    Private Declare Function getFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
    Private Declare Function getTickCount Lib "kernel32" Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long
    Private Function MicroTimer() As Double
    '
    Returns seconds.
    '
        Dim cyTicks1 As Currency
        Static cyFrequency As Currency
        '
        
    MicroTimer 0

    ' Get frequency.
        If cyFrequency = 0 Then getFrequency cyFrequency

    Get ticks.
        
    getTickCount cyTicks1

    ' Seconds
        If cyFrequency Then MicroTimer = cyTicks1 / cyFrequency
    End Function
    '
    Function TimeCell(MyRange As Rangeiterations As Integer) As Double
    Application
    .Volatile
    Dim temp 
    As Double
    Dim j 
    As Long
    Dim Temp1 
    As Variant

    If MyRange.Formula "" Then
        TimeCell 
    = -1
        
    Exit Function
    End If
    temp MicroTimer
    For 1 To iterations
        Temp1 
    Application.Evaluate(MyRange.Formula)
    Next j

    TimeCell 
    MicroTimer temp


    End 
    Function
    Function 
    FormulaIs(MyRange) As String

        FormulaIs 
    MyRange.Formula

    End 
    Function
    Private Function 
    SheetExists(SheetName As String) As Boolean

    Dim MySheet 
    As Worksheet

    SheetExists 
    False

    On Error GoTo SheetExistsError
    Set MySheet 
    Sheets(SheetName)

    SheetExists True

    SheetExistsError
    :
    On Error GoTo 0


    End 
    Function


    Sub TimeSheet()

    Application.Calculation xlCalculationManual
    Application
    .EnableEvents False
    Application
    .ScreenUpdating False

    Dim MySheet 
    As Worksheet
    Dim Mycell 
    As Range
    Dim TempSheet 
    As String
    Dim row 
    As Long
    Dim iterations 
    As Integer
    Dim Speed 
    As Double

    iterations 
    100


    TempSheet 
    ActiveSheet.Name

    If Not SheetExists("Timings"Then

        Sheets
    .Add
        ActiveSheet
    .Name "Timings"
       
    Else
        
    Sheets("Timings").Activate
        Cells
    .Clear
        Cells
    .EntireColumn.AutoFit
        Cells
    .EntireRow.AutoFit
        
    End 
    If


    Sheets(TempSheet).Activate

    Set MySheet 
    ActiveSheet
     row 
    1
     
    For Each Mycell In MySheet.UsedRange

    If Left(Mycell.Formula1) = "=" Then
    Speed 
    TimeCell(Mycelliterations) / iterations
    With Sheets
    ("Timings").Range("a1")
       .
    Offset(row1) = Mycell.Address
       
       
    .Offset(row2) = Speed
       
    .Offset(row2).NumberFormat "0.0000000"
       
       
    .Offset(row3).Formula "=" Application.WorksheetFunction.Substitute(.Offset(row2).Address"$""") & "/sum(c:c)"
       
    .Offset(row3).NumberFormat "0.0000%"
       
    .Offset(row4).Value Len(Mycell.Formula) - 1
       
    .Offset(row5) = Right(Mycell.FormulaLen(Mycell.Formula) - 1)
    End With
           row 
    row 1
    End 
    If



    Next
    Sheets
    ("Timings").Activate
    With Range
    (Range("a1").Offset(11), Range("a1").Offset(row 15))
            .
    Sort Key1:=Range("a1").Offset(02), Order1:=xlDescending

    End With

    Application
    .EnableEvents True
    Application
    .Calculation xlCalculationAutomatic
    Application
    .ScreenUpdating True
    End Sub 
    Last edited by squiggler47; 05-20-2009 at 07:34 AM.
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0