Jump to content

J.Prime


Sunaj
 Share

Recommended Posts

Hi, I created the following little program that among other things can be used to demonstrate the speed difference between the Official AU3 release and the beta (yes, there IS a 100% speed increase, run this program and check it for yourself!). Please note that J.Prime contains a quite nice (though basic) prime number checker that could find use in many other contexts (or simply for learning purposes).

Please post improvements.., speed results, etc. Enjoy :P

Sunaj

Edit: focus on primes

#include <Math.au3>
#include <GUIConstants.au3>
#include <Array.au3>
#NoTrayIcon ; hide icon from icontray
Opt("GUIOnEventMode", 1)
Opt("SendKeyDelay", 1)

GUICreate("J.Prime",330,140,-1,-1,$WS_SYSMENU,$WS_EX_TOOLWINDOW + $WS_EX_APPWINDOW)

GUICtrlCreateLabel ( _
        "This is a prime benchmark for AU3, it computes all primes from" & @CRLF & _
        "0 - 10.000 && puts the primes in a file and then returns the time" & @CRLF & _
        "used in seconds - save includes time stats for future reference.",10,10)

$h_StartButton= GUICtrlCreateButton("Start Computing",10,56,100,19)
GUICtrlSetOnEvent (-1, "func_StartButton" )

$h_ShowFileButton= GUICtrlCreateButton("Show Primes",214,56,100,19)
GUICtrlSetOnEvent (-1, "func_ShowFileButton" )
GUICtrlSetState(-1, $GUI_DISABLE)

$h_ProgressBar = GuiCtrlCreateProgress(10,85,304,19,$PBS_SMOOTH)

$h_hidden = GUICtrlCreateButton("hidden",1,1) ; fix for button focus issue, see func_ButtonfocusFix()
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetState(-1, $GUI_FOCUS)

GUISetOnEvent($GUI_EVENT_CLOSE, "func_Close")

GUISetState()

$i_CounterForProgressBar = 0

Func func_StartButton()
    func_ButtonfocusFix()
    $i_TimerStart = TimerInit()
    GUICtrlSetData ($h_StartButton,"Please Wait")
    GUICtrlSetState($h_StartButton, $GUI_DISABLE)
    Dim $a_PrimeList[1]
    $i_PrimeCeiling = 10000
    $i_PrimeCandidate = 2    
    While $i_PrimeCandidate <= $i_PrimeCeiling
        $i_Divisor = 2
        $i_PrimeTest = 1 ; assume current $i_PrimeCandidate IS a prime
        While ($i_Divisor * $i_Divisor <= $i_PrimeCandidate)
            If Mod($i_PrimeCandidate,$i_Divisor) = 0 Then
                $i_PrimeTest = 0 ; current $i_PrimeCandidate IS NOT a prime
                ExitLoop
            EndIf            
            $i_Divisor += 1
        Wend
        If $i_PrimeTest <> 0 Then
            _ArrayAdd($a_PrimeList,$i_PrimeCandidate)
        EndIf
        $i_PrimeCandidate += 1
        $i_CounterForProgressBar += 0.01 ; allow correct countup to 100 (which is max of $h_ProgressBar)
        GUICtrlSetData ($h_ProgressBar,$i_CounterForProgressBar)
    Wend
    $i_TimeUsed = TimerDiff($i_TimerStart)
    $i_TimeUsed = "Seconds used: " & $i_TimeUsed/1000
    MsgBox(4096, "Time benchmark", $i_TimeUsed)
    GUICtrlSetData ($h_ProgressBar,0) ; clear $h_ProgressBar for next run
    GUICtrlSetData ($h_StartButton,"Start Computing")
    GUICtrlSetState($h_StartButton, $GUI_ENABLE)
    _ArrayDelete ($a_PrimeList,0) ; remove empty index key in array before converting to string
    $s_PrimesString = _ArrayToString($a_PrimeList,@CRLF)
    $f_PrimesTxt = FileOpen ("primes.txt",2)    
    FileWrite($f_PrimesTxt,$i_TimeUsed & @CRLF & @CRLF)
    FileClose($f_PrimesTxt) ;close $f_PrimesTxt to allow for next line
    $f_PrimesTxt = FileOpen ("primes.txt",1) ; change write mode to append to end of $f_PrimesTxt
    FileWrite($f_PrimesTxt,$s_PrimesString)
    FileClose($f_PrimesTxt)
    GUICtrlSetState($h_ShowFileButton, $GUI_ENABLE)
EndFunc

Func func_ButtonfocusFix() ; lets focus behave as in standard windows application (does NOT leave black selection around clicked button)
    GUICtrlSetState(@GUI_CtrlId, $GUI_HIDE)
    GUICtrlSetState($h_hidden, $GUI_FOCUS)
    GUICtrlSetState(@GUI_CtrlId, $GUI_SHOW)
Endfunc

Func func_ShowFileButton() ; opens primes.txt in notepad
    func_ButtonfocusFix()
    Run("Notepad.exe " & @ScriptDir & "\primes.txt", "")
Endfunc

Func func_Close()
    Exit
EndFunc

While 1
    Sleep(500)
Wend
Edited by Sunaj
Link to comment
Share on other sites

Very big time difference! 4.95654922622847 seconds for beta and 9.01754915778402 seconds for release.

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Yea.. exactly.. rather extreme time difference! Btw, for people interested in prime numbers there is an excellent tutorial available at

Fun With Prime Numbers that teaches all the basics for programming both basic (J.Prime) and advanced prime number crunchers. The tutorial is in C++ but is relatively easily translatable into AU3 context! :P

My Intel Core Duo 1.83 (T2400) can do 3.02742527332384 for the beta and 5.91007153143766 for release.

Very big time difference! 4.95654922622847 seconds for beta and 9.01754915778402 seconds for release.

Link to comment
Share on other sites

My PC has a Pentium 4 2.8GHz CPU.

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Nice script. :P

Here are the results from my PC.

Seconds used: 15.4027590098742

2.0 ghz P4

Edited by SolidSnake
HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
Share on other sites

Link to comment
Share on other sites

  • 1 month later...

Nice, I made a faster way for you to check for primes (it makes it in less than half time) , implement it if you want to:

$searchlimit=10000

;your method
$timer=TimerInit()
$primes=$searchlimit

for $check=2 to $searchlimit
    $divisor=2
    while $check >= $divisor*$divisor
        if mod($check,$divisor)=0 Then
            $primes -=1
            exitloop
        endif
        $divisor +=1
    wend
next
msgbox(0,"your method","Speed: "&TimerDiff($timer)/1000&" Primes: "&$primes)

;my method
$timer=TimerInit()
$primes=$searchlimit/2

for $check=3 to $searchlimit step 2
    $divisor=3
    while $check >= $divisor*$divisor
        if mod($check,$divisor)=0 Then
            $primes -=1
            exitloop
        endif
        $divisor +=2
    wend
next
msgbox(0,"my method","Speed: "&TimerDiff($timer)/1000&" Primes: "&$primes)
My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer
Link to comment
Share on other sites

Your Method: 6.81 seconds

My method (modification of the prime number generation part only): 2.76 seconds

I used the same method in C++, just ported the algorithm over to AutoIt. Makes a huge difference when you go for higher primes than the limit you've specified. Tell me what you think?

There are many neat little programming tricks like this. If you like challenges, try making a program similar to this:

http://www.autoitscript.com/forum/index.php?showtopic=40627

#NoTrayIcon
AutoItSetOption("MustDeclareVars", 0)
AutoItSetOption("RunErrorsFatal", 0)
AutoItSetOption("TrayIconDebug", 0)
Opt("GUIOnEventMode", 1)
Opt("SendKeyDelay", 1)
Break(0)

#include <GUIConstants.au3>
#include <Array.au3>

GUICreate("J.Prime (Optimized by K.John)",330,140,-1,-1,$WS_SYSMENU,$WS_EX_TOOLWINDOW + $WS_EX_APPWINDOW)

GUICtrlCreateLabel ( _
        "This is a prime benchmark for AU3, it computes all primes from" & @CRLF & _
        "0 - 10.000 && puts the primes in a file and then returns the time" & @CRLF & _
        "used in seconds - save includes time stats for future reference.",10,10)

$h_StartButton= GUICtrlCreateButton("Start Computing",10,56,100,19)
GUICtrlSetOnEvent (-1, "func_StartButton" )

$h_ShowFileButton= GUICtrlCreateButton("Show Primes",214,56,100,19)
GUICtrlSetOnEvent (-1, "func_ShowFileButton" )
GUICtrlSetState(-1, $GUI_DISABLE)

$h_ProgressBar = GuiCtrlCreateProgress(10,85,304,19,$PBS_SMOOTH)

$h_hidden = GUICtrlCreateButton("hidden",1,1) ; fix for button focus issue, see func_ButtonfocusFix()
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetState(-1, $GUI_FOCUS)

GUISetOnEvent($GUI_EVENT_CLOSE, "func_Close")

GUISetState()

$i_CounterForProgressBar = 0

Func func_StartButton()
    func_ButtonfocusFix()
    $i_TimerStart = TimerInit()
    GUICtrlSetData ($h_StartButton,"Please Wait")
    GUICtrlSetState($h_StartButton, $GUI_DISABLE)
    
    Dim $primes[1]
    $primes[0] = 2
    $i = 1
    $max = 10000
    $size = 0
    
    For $i = 3 To $max Step 2
        $i_CounterForProgressBar += 0.02 ; allow correct countup to 100 (which is max of $h_ProgressBar)
        GUICtrlSetData ($h_ProgressBar,$i_CounterForProgressBar)
        $j = 0
        While $primes[$j] * $primes[$j] <= $i
            If Mod($i, $primes[$j]) = 0 Then ContinueLoop 2
            $j = $j + 1
        WEnd
        $size = $size + 1
        ReDim $primes[$size + 1]
        $primes[$size] = $i
    Next
    
    $i_TimeUsed = TimerDiff($i_TimerStart)
    $i_TimeUsed = "Seconds used: " & $i_TimeUsed/1000
    MsgBox(4096, "Time benchmark", $i_TimeUsed)
    GUICtrlSetData ($h_ProgressBar,0) ; clear $h_ProgressBar for next run
    GUICtrlSetData ($h_StartButton,"Start Computing")
    GUICtrlSetState($h_StartButton, $GUI_ENABLE)
    $s_PrimesString = _ArrayToString($primes,@CRLF)
    $f_PrimesTxt = FileOpen ("primes.txt",2)    
    FileWrite($f_PrimesTxt,$i_TimeUsed & @CRLF & @CRLF)
    FileClose($f_PrimesTxt) ;close $f_PrimesTxt to allow for next line
    $f_PrimesTxt = FileOpen ("primes.txt",1) ; change write mode to append to end of $f_PrimesTxt
    FileWrite($f_PrimesTxt,$s_PrimesString)
    FileClose($f_PrimesTxt)
    GUICtrlSetState($h_ShowFileButton, $GUI_ENABLE)
EndFunc

Func func_ButtonfocusFix() ; lets focus behave as in standard windows application (does NOT leave black selection around clicked button)
    GUICtrlSetState(@GUI_CtrlId, $GUI_HIDE)
    GUICtrlSetState($h_hidden, $GUI_FOCUS)
    GUICtrlSetState(@GUI_CtrlId, $GUI_SHOW)
Endfunc

Func func_ShowFileButton() ; opens primes.txt in notepad
    func_ButtonfocusFix()
    Run("Notepad.exe " & @ScriptDir & "\primes.txt", "")
Endfunc

Func func_Close()
    Exit
EndFunc

While 1
    Sleep(500)
Wend
Link to comment
Share on other sites

wow, I installed beta version and its much much faster

2.7 in the original way 1.2 with mine

My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...