VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "dwBenchMark"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
' dwBenchMark - Benchmarking utility class
' Part of the Desaware API Class Library
' Copyright (c) 1996 by Desaware Inc.
' All Rights Reserved

Option Explicit

#If Win32 Then

Private Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long
Attribute GetProcessTimes.VB_HelpID = 2139
Attribute GetProcessTimes.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
Private Declare Function GetTickCount Lib "kernel32" () As Long
Attribute GetTickCount.VB_HelpID = 2356
Attribute GetTickCount.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Attribute GetCurrentProcess.VB_HelpID = 2141
Attribute GetCurrentProcess.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"

Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

' This is the reference user time as marked.
Private ReferenceTime As FILETIME
' This is the marked user time for comparisons.
Private MarkTime As FILETIME

' This is the reference kernel time as marked.
Private ReferenceKTime As FILETIME
' This is the marked kernel time for comparisons.
Private MarkKTime As FILETIME

' Reference TickCount
Private ReferenceTick As Long
' The marked tick count for comparisons
Private MarkTick As Long

' A variable to hold the current process handle to use
Private ThisProcess As Long

' Dummy filetime structures that we won't actually use
Private fcreate As FILETIME
Private fexit As FILETIME

Private Sub Class_Initialize()
    ' There is no need to close this pseudo handle
    ThisProcess = GetCurrentProcess()
End Sub

' Sets the reference time
Public Sub SetReference()
    ReferenceTick = GetTickCount
    Call GetProcessTimes(ThisProcess, fcreate, fexit, ReferenceKTime, ReferenceTime)
End Sub


' Sets the mark time
Public Sub SetMark()
    MarkTick = GetTickCount
    Call GetProcessTimes(ThisProcess, fcreate, fexit, MarkKTime, MarkTime)
End Sub

' Subtracts MarkTime from ReferenceTime and returns the difference
Private Function CalculateDifference(ByVal timespec As Integer) As FILETIME
    Dim f As FILETIME
    Select Case timespec
        Case 0  ' User time
                Call agSubtractFileTimes(MarkTime, ReferenceTime, f)
        Case 1  ' Kernel time
                Call agSubtractFileTimes(MarkKTime, ReferenceKTime, f)
    End Select
    CalculateDifference = f
End Function

' Debug routine
#If DEBUGCOMPILE Then
Public Sub dbg1()
    Dim f As FILETIME
    f = CalculateDifference()
    Debug.Print Hex$(f.dwHighDateTime) & " " & Hex$(f.dwLowDateTime)
    
End Sub
#End If


' Get the difference based on the tick count in ms
Public Function GetTickDifference() As Long
    GetTickDifference = MarkTick - ReferenceTick
End Function

' Get the difference based on user time in ms
Public Function GetuserDifferenceMS() As Double
    Dim diff As FILETIME
    Dim res As Double
    diff = CalculateDifference(0)
    res = diff.dwLowDateTime / 10000
    If diff.dwHighDateTime <> 0 Then
        ' Add in the number of milliseconds for each high count
        res = res + diff.dwHighDateTime * 42949.67296
    End If
    GetuserDifferenceMS = res
End Function

Public Function GetkernelDifferenceMS() As Double
    Dim diff As FILETIME
    Dim res As Double
    diff = CalculateDifference(1)
    res = diff.dwLowDateTime / 10000
    If diff.dwHighDateTime <> 0 Then
        ' Add in the number of milliseconds for each high count
        res = res + diff.dwHighDateTime * 42949.67296
    End If
    GetkernelDifferenceMS = res
End Function

#End If ' This class exists in win32 only
