Why is BlitzPlus so much slower than Blitz2D?
BlitzPlus Forums/BlitzPlus Programming/Why is BlitzPlus so much slower than Blitz2D?
| ||
I wrote an algorithm just to see how fast C\C++ is when compared to Blitz. C/C++ did it in 1.5 secs, which didn't surprise me. Blitz2d/3D did it in 25 secs. BlitzPlus did it in 1 min 9 secs. I'm just wondering what change in BlitzPlus that it made it so much slower. Here is the code: ;this program sorts an array of numbers from ;largest to smallest Global count=1 Global temp Const terminator=-1 Print "This program sorts random numbers from 1 to 32768 from largest to smallest." Print " " Print "If you want to sort a 1000 numbers enter a (1000)." num = Input("Enter how many elements you want the array: ") Dim SortArray(num+1) SeedRnd (MilliSecs()) For a=1 To num+1 SortArray(a)=Rnd(1,32768) Next SortArray(num+1)=terminator Print "Please wait..." oldtime$ = CurrentTime() While (SortArray(count) <> terminator) If SortArray(count+1) <= SortArray(count) count=count+1 Else temp=SortArray(count+1) SortArray(count+1) = SortArray(count) SortArray(count) = temp count = 1 EndIf If KeyDown(1) Then Exit Wend newtime$ = CurrentTime() For b=1 To (num) Print SortArray(b)+" " Next Print "Start Time "+oldtime$ Print "End Time "+newtime$ Print " " FlushKeys() WaitKey() End |
| ||
I tried your program on 1000 elements in BlitzPlus. It took 22 seconds. I'm not sure what to make of that. Here's one of my old test programs from the early days of Blitz Basic(2d). I just tried it on 100,000 elements in Blitz3D and BlitzPlus. Times are about the same, a little under .3 seconds. ; Demonstration of the HeapSort algorithm. ; A large array of random integers is sorted into non-decreasing order. Graphics 640, 480, 0, 2 Global n = 1000 * 100 ; number of integers to sort Dim a(n) ; a(1..n) holds the integers, a(0) is ignored heaptest() WaitKey() End Function heaptest() ; fill in random data, do the sort and display results Local t# Text 10, 10, "Filling in random data..." initialize() Text 200, 10, "Done." Text 10, 30, "Beginning sort... " t#=MilliSecs() heapsort() t#=MilliSecs()-t# Text 200, 30, "Done." Text 10, 50, "Verifying correctness of sort: " If IsSorted() Then Text 10, 90, "Success." Text 10, 120, n+" integers sorted in "+t#/1000+" seconds" Else Text 10, 140, "Failure." ; this should never happen EndIf End Function Function initialize() ; fill array a(1..n) with some random integers Local i For i=1 To n a(i)=Rnd(1000000) Next End Function Function IsSorted() ; are a(1) to a(n) in non-decreasing order? Local i For i=1 To n-1 If a(i)>a(i+1) Then Return False ; out of order, should never happen Next Return True End Function ; ********* all the work is done with these two functions ; This is for integers but could be easily modified to handle any ; kind of data, even types. The only changes needed are to the code ; to swap two elements and the code to compare two elements. Function heapsort() ; the array a(1..n) Note: a() and n are Global Local i,n2,temp n2=n ; save a copy of the original n For i=n-1 To 1 Step -1 heapify(i) Next While n>1 temp=a(1) : a(1)=a(n) : a(n)=temp ; swap a(1),a(n) n=n-1 heapify(1) Wend n=n2 End Function Function heapify(i) ; everything below node i is already a heap Local j,k,temp j=i+i If j<=n k=j+1 If k<=n If a(k)>a(j) temp=j : j=k : k=temp EndIf EndIf If a(i)<a(j) temp=a(i) : a(i)=a(j) : a(j)=temp heapify(j) EndIf EndIf End Function Edit: I just noticed where most of the time is spent. There is a call to KeyDown() inside the sort. This is using almost 90% of the time. Getting rid of it cuts the time to 3 seconds. That's still slow, but you can blame the algorithm. |
| ||
Yep that did it. Thanks! |