Thursday, March 25, 2010

SnTT: A LotusScript StopWatch Class With Nanosecond Precision

I was doing some work in Access and wanted to time how long it took to do something. I was doing a small scale test so the timing was pretty minuscule. After poking around a bit I discovered a way to use the Windows API to count CPU clock cycles. I converted it to LotusScript since virtually nobody who reads my blog cares about Access. :-)

Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Double) As Long
Declare Function QueryPerformanceFrequency Lib "kernel32" (lpPerformanceCount As Double) As Long

Public Class StopWatch
Private m_StartTime As Double
Private m_EndTime As Double
Private m_Freq As Double
Private m_Overhead As Double

Private m_Days As Integer
Private m_Hours As Integer
Private m_Minutes As Integer
Private m_Seconds As Integer
Private m_Deci As Long
Private m_Centi As Long
Private m_Milli As Long
Private m_Micro As Long
Private m_Nano As Long

Private m_TotalSeconds As Single

Public Property Get Hours As Integer
Hours = m_Hours
End Property

Public Property Get Minutes As Integer
Minutes = m_Minutes
End Property

Public Property Get Seconds As Integer
Seconds = m_Seconds
End Property

Public Property Get Milli As Integer
Milli = m_Milli
End Property

Public Property Get Centi As Integer
Centi = m_Centi
End Property

Public Property Get Micro As Long
Micro = m_Micro
End Property

Public Property Get Nano As Long
Nano = m_Nano
End Property

Public Property Get TotalSeconds As Single
TotalSeconds = m_TotalSeconds
End Property

Public Sub StartTimer()
QueryPerformanceCounter m_StartTime
End Sub

Public Sub EndTimer()
QueryPerformanceCounter m_EndTime

Dim ElapsedTime As Double

ElapsedTime = (m_EndTime - m_StartTime - m_Overhead) / m_Freq
m_TotalSeconds = Csng(ElapsedTime)

m_Days = ElapsedTime \ 86400
If m_Days > 0 Then
ElapsedTime = ElapsedTime - m_Days * 86400
End If

m_Hours = ElapsedTime \ 3600
If m_Hours > 0 Then
ElapsedTime = ElapsedTime - m_Hours * 3600
End If

m_Minutes = ElapsedTime \ 60
If m_Minutes > 0 Then
ElapsedTime = ElapsedTime - m_Minutes * 60
End If

m_Seconds = Int(ElapsedTime)
If m_Seconds > 0 Then
ElapsedTime = ElapsedTime - m_Seconds
End If

m_Deci = Clng(Round(Clng(ElapsedTime * 10), 1))
m_Centi = Clng(Round(Clng(ElapsedTime * 100), 2))
m_Milli = Clng(Round(Clng(ElapsedTime * 1000), 3))
m_Micro = Clng(Round(Clng(ElapsedTime * 100000), 6))
m_Nano = Clng(Round(Clng(ElapsedTime * 1000000000), 9))

End Sub

Public Sub New()
Dim mStartTime As Double
Dim mEndTime As Double

'First figure out the API overhead
QueryPerformanceCounter mStartTime
QueryPerformanceCounter mEndTime

m_Overhead = mEndTime - mStartTime

'Now get the frequency
QueryPerformanceFrequency m_Freq
End Sub

End Class

This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at

Saturday, March 06, 2010

playing the hand you're dealt

To cut to the chase, I'll be starting classes at the Art Institute in July. Trust me, you couldn't possibly be any more surprised than I am.

By now you know I was selected as one of five finalists in a Food Network and Art Institutes scholarship contest, but I couldn't accept because the cost of acceptance was too high. The events that led from there to here still seem like a dream.

The contest rules state the scholarship can only be used for an associate or bachelor degree program, which cost $53,000 and $80,000, respectively. When I spoke with the contest coordinator I explained I couldn't cover the gap between the scholarship and the total cost of the degree. She took this back to the Food Network and AI, and they agreed to change the rules of the scholarship.

The Art Institute also offers a culinary arts certificate program, which is only three quarters long and only covers the core culinary classes. It also only costs $23,000, and I can cover $3,000 a lot more easily than $33,000, so I accepted the scholarship.

Even though I have known I was the winner for a while I was waiting for someone to pull the rug from under me. Stuff like this doesn't happen to me. As of yesterday, it's really official: Food Network Scholarship Announcement.