Jump to content
Sign in to follow this  
andybiochem

Script getting slower and slower

Recommended Posts

andybiochem

Hi!,

I have been playing with Double Buffering graphics controls, and wanted to create a script that shows the growth of a normal distribution.

Here is the code:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode",2)

GUICreate("",500,300,-1,-1,-1,0x2000000)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Close")

$g = GUICtrlCreateGraphic(0,0,500,300)
GUICtrlSetBkColor(-1,0xFFFFFF)
GUICtrlSetGraphic($g,$GUI_GR_PENSIZE,1)

$l = GUICtrlCreateLabel("n = ",10,10,100,15)
GUICtrlSetBkColor(-1,$GUI_BKCOLOR_TRANSPARENT)
$lc = 0

Dim $a[501]


GUISetState()
While 1
    $a[_Random_Gaussian(250,50)] += 1
    For $i = 1 to 500
        If $i = 1 Then GUICtrlSetGraphic($g,$GUI_GR_MOVE,$i,250-$a[$i])
        GUICtrlSetGraphic($g,$GUI_GR_MOVE,$i,250)
        GUICtrlSetGraphic($g,$GUI_GR_LINE,$i,250-$a[$i])
    Next
    GUICtrlSetGraphic($g,$GUI_GR_REFRESH)
    
    $lc += 1
    GUICtrlSetData($l,"n = " & $lc)
WEnd


Func _Random_Gaussian($nMean,$nSD)
;******************************************************************
; Box-Muller polar transform gaussian RND
;******************************************************************
; $iMean = The mean of the distribution
; $iSD = desired standard deviation
    Do
        $nX = ((2 * Random()) - 1)
        $nY = ((2 * Random()) - 1)
        $nR = ($nX^2) + ($nY^2)
    Until $nR < 1
    Return (($nX * (Sqrt((-2 * (Log($nR) / $nR))))) * $nSD) + $nMean
EndFunc


Func _close()
    exit
EndFunc

Problem is, the program starts really quickly populating the graphic, but then gets slower and slower when you get to the 1000's of iterations. :mellow:

Anybody know why?

Is there a fix?

Thanks!!!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites
Malkey

Hi!,

I have been playing with Double Buffering graphics controls, and wanted to create a script that shows the growth of a normal distribution.

Here is the code:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode",2)

GUICreate("",500,300,-1,-1,-1,0x2000000)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Close")

$g = GUICtrlCreateGraphic(0,0,500,300)
GUICtrlSetBkColor(-1,0xFFFFFF)
GUICtrlSetGraphic($g,$GUI_GR_PENSIZE,1)

$l = GUICtrlCreateLabel("n = ",10,10,100,15)
GUICtrlSetBkColor(-1,$GUI_BKCOLOR_TRANSPARENT)
$lc = 0

Dim $a[501]


GUISetState()
While 1
    $a[_Random_Gaussian(250,50)] += 1
    For $i = 1 to 500
        If $i = 1 Then GUICtrlSetGraphic($g,$GUI_GR_MOVE,$i,250-$a[$i])
        GUICtrlSetGraphic($g,$GUI_GR_MOVE,$i,250)
        GUICtrlSetGraphic($g,$GUI_GR_LINE,$i,250-$a[$i])
    Next
    GUICtrlSetGraphic($g,$GUI_GR_REFRESH)
    
    $lc += 1
    GUICtrlSetData($l,"n = " & $lc)
WEnd


Func _Random_Gaussian($nMean,$nSD)
;******************************************************************
; Box-Muller polar transform gaussian RND
;******************************************************************
; $iMean = The mean of the distribution
; $iSD = desired standard deviation
    Do
        $nX = ((2 * Random()) - 1)
        $nY = ((2 * Random()) - 1)
        $nR = ($nX^2) + ($nY^2)
    Until $nR < 1
    Return (($nX * (Sqrt((-2 * (Log($nR) / $nR))))) * $nSD) + $nMean
EndFunc


Func _close()
    exit
EndFuncoÝ÷ ØúènW¦Ëaz趦²Ö«¶ÊÞjYrªè©¢¥jاØ^¶©'ºÛazx¶Ë%£«jwl¯^*.ëm¢Ø^×M4ßÛ(~+^­«b¢{&iÐ'ɺÊIèÃr"Ëaz·~,S©ä³}÷ß}ÿªê-y8b²Ê^yÛax,ºá{
+vÚ,yªÜ磬ªê-~Xêâ
®¢Ö®¶­sb6æ6ÇVFRfÇC´uT6öç7FçG4WæS2fwC°¢6æ6ÇVFRfÇCµväæS2fwC°¢6æ6ÇVFRfÇCµtäDõu46öç7FçG2æS2fwC° ¤÷BgV÷C´uTöäWfVçDÖöFRgV÷C²Â¤÷BgV÷C´Ö÷W6T6ö÷&DÖöFRgV÷C²Â" ¢b33c¶wVÒuT7&VFRgV÷C²gV÷C²ÂSÂ3ÂÓÂÓÂÓÂ#¤uT6WDöäWfVçBb33c´uTôUdTåEô4Äõ4RÂgV÷Cµô6Æ÷6RgV÷C² ¢b33c¶rÒuT7G&Ä7&VFTw&2ÂÂSÂ3£´uT7G&Å6WD&´6öÆ÷"ÓÃdddddb¤uT7G&Å6WDw&2b33c¶rÂb33c´uTôu%õTå4¤R ¢b33c¶ÂÒuT7G&Ä7&VFTÆ&VÂgV÷C¶âÒgV÷C²ÂÂÂÂR£´uT7G&Å6WD&´6öÆ÷"ÓÂb33c´uTô$´4ôÄõ%õE$å5$TåB¢b33c¶Æ2Ò ¤FÒb33c¶³SÐ  ¤uT6WE7FFR¥vÆR uT7G&ÄFVÆWFRb33c¶r b33c¶rÒuT7G&Ä7&VFTw&2ÂÂSÂ3 ´uT7G&Å6WD&´6öÆ÷"ÓÃdddddb uT7G&Å6WDw&2b33c¶rÂb33c´uTôu%õTå4¤R b33c¶µõ&æFöÕôvW76â#SÂSÒ³Ò f÷"b33c¶ÒFòS bb33c¶ÒFVâuT7G&Å6WDw&2b33c¶rÂb33c´uTôu%ôÔõdRÂb33c¶Â#SÒb33c¶²b33c¶Ò uT7G&Å6WDw&2b33c¶rÂb33c´uTôu%ôÔõdRÂb33c¶Â#S uT7G&Å6WDw&2b33c¶rÂb33c´uTôu%ôÄäRÂb33c¶Â#SÒb33c¶²b33c¶Ò æW@ uT7G&Å6WDw&2b33c¶rÂb33c´uTôu%õ$Te$U4  b33c¶Æ2³Ò uT7G&Å6WDFFb33c¶ÂÂgV÷C¶âÒgV÷C²fײb33c¶Æ2  6ÆVW¥tVæ@  ¤gVæ2õ&æFöÕôvW76âb33c¶äÖVâÂb33c¶å4B Æö6Âb33c¶åÂb33c¶åÂb33c¶å  ²¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢  ²&÷Ô×VÆÆW"öÆ"G&ç6f÷&ÒvW76â$ä@ ²¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢  ²b33c¶ÖVâÒFRÖVâöbFRF7G&'WFöà ²b33c¶4BÒFW6&VB7FæF&BFWfFöà Fð b33c¶åÒ"¢&æFöÒÒ b33c¶åÒ"¢&æFöÒÒ b33c¶å"Òb33c¶åâ"²b33c¶åâ" VçFÂb33c¶å"fÇC² &WGW&âb33c¶å¢7'"¢Æörb33c¶å"òb33c¶å"¢b33c¶å4B²b33c¶äÖVà¤VæDgVæ2³ÓÒfwCµõ&æFöÕôvW76à  ¤gVæ2ö6Æ÷6R W@¤VæDgVæ2³ÓÒfwCµö6Æ÷6P

Share this post


Link to post
Share on other sites
PsaltyDS

You don't have to update the graphic display every time you update the data. Try it this way:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WINDOWSConstants.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)

$hGui = GUICreate("", 500, 300, -1, -1, -1, 0x2000000)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")

$g = GUICtrlCreateGraphic(0, 0, 500, 300)
;GUICtrlSetBkColor(-1,0xFFFFFF)
GUICtrlSetGraphic($g, $GUI_GR_PENSIZE, 1)

$l = GUICtrlCreateLabel("n = ", 10, 10, 100, 15)
;GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
$lc = 0

Dim $a[501]
GUISetState()

$iTimer = TimerInit()
While 1
    $a[_Random_Gaussian(250, 50)] += 1
    $lc += 1
    GUICtrlSetData($l, "n = " & $lc)

; Update graphic once per second
    If TimerDiff($iTimer) >= 1000 Then
        $iTimer = TimerInit()
        GUICtrlDelete($g)
        $g = GUICtrlCreateGraphic(0, 0, 500, 300)
    ;GUICtrlSetBkColor(-1,0xFFFFFF)
        GUICtrlSetGraphic($g, $GUI_GR_PENSIZE, 1)
        For $i = 1 To 500
            If $i = 1 Then GUICtrlSetGraphic($g, $GUI_GR_MOVE, $i, 250 - $a[$i])
            GUICtrlSetGraphic($g, $GUI_GR_MOVE, $i, 250)
            GUICtrlSetGraphic($g, $GUI_GR_LINE, $i, 250 - $a[$i])
        Next
        GUICtrlSetGraphic($g, $GUI_GR_REFRESH)
    EndIf
WEnd

Func _Random_Gaussian($nMean, $nSD)
    Local $nX, $nY, $nR
;******************************************************************
; Box-Muller polar transform gaussian RND
;******************************************************************
; $iMean = The mean of the distribution
; $iSD = desired standard deviation
    Do
        $nX = ((2 * Random()) - 1)
        $nY = ((2 * Random()) - 1)
        $nR = ($nX ^ 2) + ($nY ^ 2)
    Until $nR < 1
    Return (($nX * (Sqrt((-2 * (Log($nR) / $nR))))) * $nSD) + $nMean
EndFunc  ;==>_Random_Gaussian

Func _close()
    Exit
EndFunc  ;==>_close

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
andybiochem

Hi guys, thanks for the responses!!

Looks like there are two reasons why the script was slowing.

1) The graphic control seems to become 'saturated' at about 500,000 calls to GUICtrlSetGraphic, so needs clearing to maintain speed (deleting>re-creating)

2) My dopey code wasn't elegant enough to minimise the setgraphic calls...I'm looping through the whole array each time unneccessarily! oops.

This works much better:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("",500,300,-1,-1,-1,0x2000000)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Close")

$g = GUICtrlCreateGraphic(0,0,500,300)
GUICtrlSetBkColor(-1,0xFFFFFF)
GUICtrlSetGraphic($g,$GUI_GR_PENSIZE,1)

$l = GUICtrlCreateLabel("n = ",10,10,100,15)
GUICtrlSetBkColor(-1,$GUI_BKCOLOR_TRANSPARENT)
$lc = 0

Dim $a[501]

GUISetState()
While 1
    $rnd = _Random_Gaussian(250,50)
    $a[$rnd] += 1
    GUICtrlSetGraphic($g,$GUI_GR_MOVE,$rnd,250)
    GUICtrlSetGraphic($g,$GUI_GR_LINE,$rnd,250-$a[$rnd])
    
    GUICtrlSetGraphic($g,$GUI_GR_REFRESH)
    
    $lc += 1
    GUICtrlSetData($l,"n = " & $lc)

WEnd


Func _Random_Gaussian($nMean,$nSD)
;******************************************************************
; Box-Muller polar transform gaussian RND
;******************************************************************
; $iMean = The mean of the distribution
; $iSD = desired standard deviation
    Do
        $nX = ((2 * Random()) - 1)
        $nY = ((2 * Random()) - 1)
        $nR = ($nX^2) + ($nY^2)
    Until $nR < 1
    Return (($nX * (Sqrt((-2 * (Log($nR) / $nR))))) * $nSD) + $nMean
EndFunc

Func _close()
    exit
EndFunc

Thanks again!!

There's also a clever 'trick' with the Random Number Generator function too that could speed things up...Can anyone guess what it is???

(Hint: a PAIR of random numbers are generated with each call to the function)


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×