The routine runs through about 10 million calcs in two versions. There should be several processor cycles difference
in the for/next loops. Thus I expected about half a millisecond (0.5) difference in the timing between the two calcs.
Then -- what would the time be for a thread?
It just seemed that threads would require less time.
HOWEVER!
The thread calls use approx 30 millisecs more than a subroutine call.
Therefore: 1st question, what am I doing wrong?
Any help/explanation is appreciated.
This is the snippet:
Code: Select all
'' ThreadExample_04f.bas
/'
Are threads isolated from system interrupts?
'/
Dim Shared terminate As Integer = 0
Dim Shared mutex As Any Ptr
dim shared ThreadTime1 as double = 0
dim shared ThreadTime2 as double = 0
dim shared MilliSecsThread1 as double = 1000.00#
dim shared MilliSecsThread2 as double = 1000.00#
dim shared SubRouteTime1 as double = 1234.00#
dim shared SubRouteTime2 as double = 4321.00#
SUB Routine1 (ByVal param As Any Ptr)
dim as double t1, t0 = timer
dim as longint i, j, k, sum
for i = 1ul to 100ul
for j = 1ul to 100ul
for k = 1ul to 1000ul
sum = sum + 1ul
next k
next j
next i
t1 = timer
SubRouteTime1 = (t1 - t0) * 1000.0#
END SUB
Sub Routine2(ByVal param As Any Ptr)
dim as double t1, t0 = timer
dim as longint i, sum
for i = 1ul to 10000000ul
sum = sum + 1ul
next i
t1 = timer
SubRouteTime2 = (t1 - t0) * 1000.0#
end sub
Sub thread1(ByVal param As Any Ptr)
dim as double t1, t0=timer
dim as longint i, j, k, sum
for i = 1 to 100ul
for j = 1 to 100ul
for k = 1 to 1000ul
sum = sum + 1ul
next k
next j
next i
t1 = timer
MilliSecsThread1 = (t1 - t0) * 1000.0#
End Sub
Sub thread2 (ByVal param As Any Ptr)
dim as double t1, t0 = timer
dim as longint i, j, k, sum
for i = 1 to 10000000ul
sum = sum + 1ul
next i
t1 = timer
MilliSecsThread2 = (t1 - t0) * 1000.0#
End Sub
'ScreenSet 1, 0
dim as double t2, t1, t0=timer
dim as longint i, j, k, sum, z
dim p0 as any ptr
for example as integer = 1 to 7
print
print "============================"
mutex = MutexCreate
Dim p1 As Any Ptr = ThreadCreate(@thread1)
Dim p2 As Any Ptr = ThreadCreate(@thread2)
ThreadWait (p1)
ThreadWait (p2)
MutexDestroy(mutex)
print
print "1st thread millisec: "; MilliSecsThread1
print "2nd thread millisec: "; MilliSecsThread2
Routine1(@thread1)
Routine2(@thread1)
print " "
print "1st Subr. millisec: "; SubRouteTime1
print "2nd Subr. millisec: "; SubRouteTime2
next example
print
Sleep
- ============================
1st thread millisec: 118.6101293133106
2nd thread millisec: 108.4680745843798
1st Subr. millisec: 84.03204906062456
2nd Subr. millisec: 83.2177554621012
============================
1st thread millisec: 113.362805925135
2nd thread millisec: 108.5327140317531
1st Subr. millisec: 83.11773875902873
2nd Subr. millisec: 83.36668548872694
============================
1st thread millisec: 112.129521148745
2nd thread millisec: 108.6146588277188
1st Subr. millisec: 82.2372716065729
2nd Subr. millisec: 81.33534752414562
============================
1st thread millisec: 111.3291545843822
2nd thread millisec: 108.596812140604
1st Subr. millisec: 80.87254828569712
2nd Subr. millisec: 82.49425817484735
============================
1st thread millisec: 109.790080532548
2nd thread millisec: 110.5830765882274
1st Subr. millisec: 81.50618391664466
2nd Subr. millisec: 80.93303715577349
============================
1st thread millisec: 108.683260041289
2nd thread millisec: 112.9249508812791
1st Subr. millisec: 106.5744509833166
2nd Subr. millisec: 106.3130789261777
============================
1st thread millisec: 113.0837071395945
2nd thread millisec: 109.0830341490801
1st Subr. millisec: 92.11460369260749
2nd Subr. millisec: 80.87781627546065
============================