Sign in to follow this  
Followers 0
E1M1

How to speed this loop? (current cpu usage is < 2%)[See Solution]

8 posts in this topic

#1 ·  Posted (edited)

[solution]

Compiled and moved it to windows XP machine (Virtual box)

[EndSolution]

How to sleed this loop up? It does about 20 loop/sec and needs lot's of time thatway. CPU usage is below 2% which means that that loop could be alot faster but for some reason it doesn't use 100% of cpu. Is there any delays? or what thing makes it so slow?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 312, 110, 192, 124)
$Teisenda = GUICtrlCreateButton("Teisenda", 208, 72, 75, 25, $WS_GROUP)
$Pic1 = GUICtrlCreatePic("C:\Users\rain\Desktop\pühad.bmp", 0, 0, 198, 99, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Teisenda
            $startpos = WinGetPos("Form1")
;~          MsgBox(0,$startpos[0]+3,$startpos[1]+24)
;~          Offsets for windows 7 theme
            _Area2HTML($startpos[0] + 3, $startpos[1] + 24, 198, 99)
    EndSwitch
WEnd




Func _Area2HTML($startX, $startY, $endX, $endY)
MsgBox(0,0,"start")
$times = 0
$data = ""
    For $y = 1 To $endY
        For $x=1 To $endX
            $times += 1
            ToolTip($times,0,0)
            $data &= '<font color="#'&Hex(PixelGetColor($startX+$x, $startY+$y), 6)&'">'&Random(0,1,1)&'</font>'
        Next
        $data &= "<br>"
    Next
    MsgBox(0,"Done","Done")
    FileWrite("pic.html",$data)
EndFunc   ;==>_Area2HTML
Edited by E1M1

edited

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

50ms per loop is slow?

Edit: NVM

I may have a solution pending.

Edit: Check THIS out.

Edited by jebus495

Share this post


Link to post
Share on other sites

but If ther's no Sleep(X) thenshouldn't it use 100% CPU?


edited

Share this post


Link to post
Share on other sites

but If ther's no Sleep(X) thenshouldn't it use 100% CPU?

Gotto love the Helpfile:

GUIGetMsg()

Remarks

This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Ok I took GUIGetMsg() out from my code but it's still slow like it was before.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 312, 110, 192, 124)
$Teisenda = GUICtrlCreateButton("Teisenda", 208, 72, 75, 25, $WS_GROUP)
$Pic1 = GUICtrlCreatePic("C:\Users\rain\Desktop\pühad.bmp", 0, 0, 198, 99, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;~ While 1
;~  $nMsg = GUIGetMsg()
;~  Switch $nMsg
;~      Case $GUI_EVENT_CLOSE
;~          Exit
;~      Case $Teisenda
$startpos = WinGetPos("Form1")
Sleep(5000)
;~          MsgBox(0,$startpos[0]+3,$startpos[1]+24)
            _Area2HTML($startpos[0] + 3, $startpos[1] + 24, 198, 99)
;~  EndSwitch
;~ WEnd




Func _Area2HTML($startX, $startY, $endX, $endY)
MsgBox(0,0,"start")
$times = 0
$data = ""
    For $y = 1 To $endY
        For $x=1 To $endX
            $times += 1
            ToolTip($times,0,0)
            $data &= '<font color="#'&Hex(PixelGetColor($startX+$x, $startY+$y), 6)&'">'&Random(0,1,1)&'</font>'
        Next
        $data &= "<br>"
    Next
    MsgBox(0,"Done","Done")
    FileWrite("pic.html",$data)
EndFunc   ;==>_Area2HTML

edited

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Why are these msgbox() statements in there?

How much data is generated in the loops?

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Looks like PixelGetColor() doesn't allow autoit to use 100% of cpu.

Following loop goes alot faster and uses 100% of cpu

Func _Area2HTML($startX, $startY, $endX, $endY)
MsgBox(0,0,"start")
$times = 0
$data = ""
    For $y = 1 To $endY
        For $x=1 To $endX
            $times += 1
            ToolTip($times,0,0)
;~          $data &= '<font color="#'&Hex(PixelGetColor($startX+$x, $startY+$y), 6)&'">'&Random(0,1,1)&'</font>'
            $data &= '<font color="#000000">0</font>'
;~          $data &= '<font color="#'&PixelGetColor($startX+$x, $startY+$y)&'">0</font>'
        Next
        $data &= "<br>"
    Next
    MsgBox(0,"Done","Done")
    FileWrite("pic.html",$data)
EndFunc   ;==>_Area2HTML

edited

Share this post


Link to post
Share on other sites

Hi,

this could be a solution if you don´t want to display the file on the screen (or if you don´t want to use PixelGetColor() )

#include <GDIPlus.au3>
#include <GDIPlusConstants.au3>

$BMPfile = "mona-lisa.jpg"
$t = TimerInit()
_Area2HTML($BMPfile, 1, 1, 198, 99) ;
$m = TimerDiff($t)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $m = ' & $m & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ShellExecute("pic.html")


Func _Area2HTML($BMPfile, $startX, $startY, $endX, $endY) ;Bitmapfiles, 24bpp only
    _GDIPlus_Startup()
    $pBitmap         = _GDIPlus_BitmapCreateFromFile($bmpfile)
    If @error Then MsgBox(0, "", "Error BitmapCreateFromFile")
    $BitmapData     = _GDIPlus_BitmapLockBits($pBitmap, 0, 0, _GDIPlus_ImageGetWidth($pBitmap), _GDIPlus_ImageGetHeight($pBitmap), $GDIP_ILMREAD, $GDIP_PXF24RGB)
    If @error Then MsgBox(0, "", "Error locking region " & @error)
    $stride         = DllStructGetData($BitmapData, "Stride")    ;Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
    $Width             = DllStructGetData($BitmapData, "Width")    ;Image width - Number of pixels in one scan line of the bitmap.
    $Height         = DllStructGetData($BitmapData, "Height")    ;Image height - Number of scan lines in the bitmap
    $ptr            = DllStructGetData($BitmapData, "Scan0")    ;Scan0 - Pointer to the first (index 0) scan line of the bitmap.

    If $startX < 1 Or $startY < 1 Or $endX > $width Or $endY > $height Then
        MsgBox(0, "Error _Aera2HTML", "Selected Area not possible!", 1)
            _GDIPlus_BitmapUnlockBits($pBitmap, $BitmapData)
            _GDIPlus_ImageDispose($pBitmap)
            _GDIPlus_Shutdown()
        return SetError(1, 0, 1)
    EndIf

    $data = ""
    $htmlpixel = Chr(129) ;take some other

    For $y = $startY-1 To $endY-1
        For $x = $startX-1 To $endX-1
            ;$stride * $y + 3 * $x     this is the position of the pixel in the bmp
            $substruct     = DllStructCreate("dword", $ptr + $stride * $y + 3 * $x)     ;read 4 bytes BBGGRRXX, little endian
            $color         = Hex(DllStructGetData($substruct, 1), 6)         ;we need only the last 3 bytes from XXRRGGBB
            $data         &= '<font color="#' & $color & '">' & $htmlpixel & '</font>'
        Next
        $data &= "<br>"
    Next
    FileDelete("pic.html")
    FileWrite("pic.html", $data)
    _GDIPlus_BitmapUnlockBits($pBitmap, $BitmapData)
    _GDIPlus_ImageDispose($pBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>_Area2HTML
 

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  
Followers 0