Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's up with my use of Timer?
#1
I'm trying to build a prog to compare different sorting algorithms (yes, I know it's been done a zillion times).
But when I try to use Timer to find the execution time, it seems to go back to the Start time, giving me expired time of zero. 
I've experimented with _Limit, to see if the system was having problems keeping up or something, and these give some expired time,
but these depend on what _limit I use, so they alter the readings.
In the small sample here, what am I missing?
Code: (Select All)
DefDbl T
Randomize Timer

MakeList:
Print Tab(30); "Create 104 Unsorted elements (4 sets A to Z)"
Dim EL$(104)
For a = 1 To 26
   For b = 0 To 3
      EL$(a + b * 26) = Chr$(Int(Rnd * 26) + 65)
      Print EL$(a + b * 26); " ";
   Next
Next

T1 = Timer '                                  start time
Print: Print "Start time is"; T1
BubbleSort:
pass = pass + 1
For a = 1 To UBound(EL$) - 1 '                                              for first element to last-1 element of Orig$() array
   ' _Limit 30
   '_limit 300
   '_limit 3000
   ' _Limit 30000
   If EL$(a) > EL$(a + 1) Then '                                          if this element is greater than next element, swap them,
      Swap EL$(a), EL$(a + 1)
      swop = 1 '                                                              and flag that a swap was made in this pass
   End If
Next
If swop = 1 Then swop = 0: GoTo BubbleSort '
T2 = Timer ' finish time
Print: Print "Finish time is"; T2

For a = 1 To 104: Print EL$(a); " ";: Next
Print: Print: Print "T1 is"; T1, "T2 is"; T2
Print: Print Tab(10); "Sorted in "; T2 - T1; "seconds, with "; pass; "passes"
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
To answer your question -- You simply don't have enough data for it to register for how long it takes to sort.   104 elements are sorted in 0.0000000000000000000001 seconds, or so, and your variable type only holds 0.000000001 seconds, or so.  It's done before the timer clicks, so 0 zeros is the correct answer.

Increase the data pool and you'll see the time increase.

Code: (Select All)
DEFDBL T
RANDOMIZE TIMER

MakeList:
PRINT TAB(30); "Create 104 Unsorted elements (301 sets A to Z)"
DIM EL$(301 * 26)
FOR a = 1 TO 26
FOR b = 0 TO 300
EL$(a + b * 26) = CHR$(INT(RND * 26) + 65)
PRINT EL$(a + b * 26); " ";
NEXT
NEXT

T1 = TIMER ' start time
PRINT: PRINT "Start time is"; T1
BubbleSort:
pass = pass + 1
FOR a = 1 TO UBOUND(EL$) - 1 ' for first element to last-1 element of Orig$() array
' _Limit 30
'_limit 300
'_limit 3000
' _Limit 30000
IF EL$(a) > EL$(a + 1) THEN ' if this element is greater than next element, swap them,
SWAP EL$(a), EL$(a + 1)
swop = 1 ' and flag that a swap was made in this pass
END IF
NEXT
IF swop = 1 THEN swop = 0: GOTO BubbleSort '
T2 = TIMER ' finish time
PRINT: PRINT "Finish time is"; T2

FOR a = 1 TO 104: PRINT EL$(a); " ";: NEXT
PRINT: PRINT: PRINT "T1 is"; T1, "T2 is"; T2
PRINT: PRINT TAB(10); "Sorted in "; T2 - T1; "seconds, with "; pass; "passes"
Reply
#3
Huh! So simple!
Oh well, at least my programme works! Rolleyes
I thought 100 or so was a reasonable set of data.  Message to self: think big!
Now, on to the other sorts.
Thanks Steve!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
...or buy the computer I'm using. Hell, cut your data in half and it will still register at least 3-seconds. Sad 

FREE SHIPPING!!!!

Pete
Reply
#5
(06-25-2024, 03:45 AM)Pete Wrote: ...or buy the computer I'm using. Hell, cut your data in half and it will still register at least 3-seconds. Sad 

FREE SHIPPING!!!!

Pete

Does it still have its wind-up handle? If not, no thanks; it may be a bit hard to get a new one.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#6
No, but the squirrel drive went down last month when I forgot to oil the hamster wheel.

Pete
Reply




Users browsing this thread: 1 Guest(s)