SortArray Micro Bench (for fun)

Everything else that doesn't fall into one of the other PB categories.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

SortArray Micro Bench (for fun)

Post by Simo_na »

Code: Select all

EnableExplicit
Global Dim test.i(15000),n.i,r.i,stime.i,etime.i,num.i=15000,Text_0,j.i,Text_1,String_0,Event
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, "Wait...")
stime = ElapsedMilliseconds()  
For j =1 To num
r=  Random(num*10)
test (n) = r
n+1
Next
SortArray(test(), #PB_Sort_Ascending  )
For n =0 To num-1
RandomizeArray(test())
SortArray(test(), #PB_Sort_Ascending  )
Next
etime = ElapsedMilliseconds()-stime 
DisableGadget(Text_0, 1)
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
EndSelect
Until Event = #PB_Event_CloseWindow
Image
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: SortArray Micro Bench (for fun)

Post by Simo_na »

Floating version

Code: Select all

EnableExplicit
Global Dim test.f(15000),n.i,r.i,stime.i,etime.i,num.i=5500,Text_0,j.i,Text_1,String_0,Event,cnt.i,flt.f=3.14
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, "Wait...")
stime = ElapsedMilliseconds()  
For j =1 To num
r=Random(num*25)
test(n)=r*flt
n+1
Next
For n =0 To num-1
SortArray(test(), #PB_Sort_Descending)
RandomizeArray(test())
SortArray(test(), #PB_Sort_Ascending)
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, Str(num-1) + " -> "+Str(n))
cnt+1
If cnt>1000
While WindowEvent(): Wend
cnt=0
EndIf
Next
etime = ElapsedMilliseconds()-stime 
DisableGadget(Text_0, 1)
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
EndSelect
Until Event = #PB_Event_CloseWindow
User_Russian
Addict
Addict
Posts: 1630
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: SortArray Micro Bench (for fun)

Post by User_Russian »

Your code creates a lot of gadgets. I fixed the bug.

Code: Select all

EnableExplicit

Global Dim test.f(15000),n.i,r.i,stime.i,etime.i,num.i=5500,Text_0,j.i,Text_1,String_0,Event,cnt.i,flt.f=3.14
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, "Wait...")
DisableDebugger
stime = ElapsedMilliseconds()  
For j =1 To num
  r=Random(num*25)
  test(n)=r*flt
  n+1
Next

For n =0 To num-1
  SortArray(test(), #PB_Sort_Descending)
  RandomizeArray(test())
  SortArray(test(), #PB_Sort_Ascending)
  If n%100=0 Or n=num
    SetGadgetText(Text_0, Str(num-1) + " -> "+Str(n))
    While WindowEvent(): Wend
  EndIf
Next
etime = ElapsedMilliseconds()-stime
EnableDebugger
DisableGadget(Text_0, 1)
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
  EndSelect
Until Event = #PB_Event_CloseWindow
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: SortArray Micro Bench (for fun)

Post by Simo_na »

ok :D
added a little bit improved math float

Code: Select all

EnableExplicit
DisableDebugger
Global Dim test.f(15000),n.i,r.i,stime.i,etime.i,num.i=5500,Text_0,j.i,Text_1,String_0,Event,cnt.i,flt.f=3.14159,nind.i=32768*100,EventID
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, "Wait...")
stime = ElapsedMilliseconds()  
For j =1 To num
r=Random(num*25)
test(n)=(Tan(flt)+(r*flt)/Sin(flt))/nind
n+1
Next
For n =0 To num-1
SortArray(test(), #PB_Sort_Descending)
RandomizeArray(test())
SortArray(test(), #PB_Sort_Ascending)
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, Str(num-1) + " -> "+Str(n))
cnt+1
If cnt>1000
While WindowEvent(): Wend
cnt=0
EndIf
Next
etime = ElapsedMilliseconds()-stime 
DisableGadget(Text_0, 1)
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
EventID = WaitWindowEvent()
Until EventID = #PB_Event_CloseWindow
User_Russian
Addict
Addict
Posts: 1630
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: SortArray Micro Bench (for fun)

Post by User_Russian »

Simo_na wrote:ok
But in your new code, the exact same bug! :shock: :shock:
It creates a lot of gadgets.

Code: Select all

Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, Str(num-1) + " -> "+Str(n))
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: SortArray Micro Bench (for fun)

Post by Simo_na »

sorry :oops:

Code: Select all

EnableExplicit
DisableDebugger
Global Dim test.f(15001),n.i,r.i,stime.i,etime.i,num.i=15000,Text_0,j.i,Text_1,String_0,Event,cnt.i,flt.f=3.14159,nind.i=32768*100,EventID
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, "Wait...")
stime = ElapsedMilliseconds()  
For j =1 To num
r=Random(num*25)
test(n)=(Tan(flt)+(r*flt)/Sin(flt))/nind
n+1
Next
For n =0 To num-1
SortArray(test(), #PB_Sort_Descending)
RandomizeArray(test())
SortArray(test(), #PB_Sort_Ascending)
cnt+1
If cnt>1000
Text_0 = TextGadget(#PB_Any, 5, 5, 100, 25, Str(num-1) + " -> "+Str(n))
While WindowEvent(): Wend
cnt=0
EndIf
Next
etime = ElapsedMilliseconds()-stime 
DisableGadget(Text_0, 1)
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
EventID = WaitWindowEvent()
Until EventID = #PB_Event_CloseWindow 
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: SortArray Micro Bench (for fun)

Post by Simo_na »

ok

Code: Select all

EnableExplicit
DisableDebugger
Global Dim test.f(15001),n.i,r.i,stime.i,etime.i,num.i=15000,j.i
Global Text_1,String_0,Event,cnt.i,flt.f=0.69314718055994529,nind.f=9792.5885,EventID
OpenWindow(#PB_Any, 40, 40, 225, 90, "SortArray Micro Bench")
stime = ElapsedMilliseconds()  
For j =1 To num
r=Random(num*25)
test(n)=Sqr((Tan(flt)+(r*flt)/Sin(flt))/(nind*flt))
n+1
Next
Text_1 = TextGadget(#PB_Any, 5, 5, 150, 25, "")
For n =0 To num-1
SortArray(test(), #PB_Sort_Descending)
RandomizeArray(test())
SortArray(test(), #PB_Sort_Ascending)
cnt+1
If cnt>1000
SetGadgetText(Text_1,StrF(test(n))+Chr(13)+Str(num-1)+" -> "+Str(n))
While WindowEvent(): Wend
cnt=0
EndIf
Next
etime = ElapsedMilliseconds()-stime 
SetGadgetText(Text_1,"")
Text_1 = TextGadget(#PB_Any, 28, 35, 100, 25, "Milliseconds")
String_0 = StringGadget(#PB_Any, 120, 25, 80, 40, Str (etime))
Repeat
EventID = WaitWindowEvent()
Until EventID = #PB_Event_CloseWindow 
Post Reply