Jump to content

Functions Speed Comparator


Colduction
 Share

Recommended Posts

Hi guys!, recently i needed to measure two functions and detect fastest one then i decided to write and share this tiny script for compare, measure and detect fastest functions easily.

It's such as snippets, i hope you find it useful :)❤
 

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#include-once
; #FUNCTION# ====================================================================================================================
; Name...........:  _FuncSpeedComparator
; Description ...:  Compares and measures speed of two functions and shows fastest one.
; Syntax.........:  _FuncSpeedComparator($s1stFunc, $s2ndFunc [,$iCallNum, $iResultType])
; Parameters ....:  $s1stFunc - First function that should be compared and measured
;                   $s2ndFunc - Second function that should be compared and measured
;                   $iCallNum - [Optional] Number of times function should be called
;                               Default value is 100
;                   $iResultType - [Optional] Type of output must be one of the following numbers:
;                                   1       ; Results will written in AutoIt Console Output
;                                   2       ; Results will be as a Message Box
;                                   3       ; Results will written in AutoIt Console Output and then shows as a Message Box
;                                   Default value is 1
; Return values .:  Success - Returns string
;                   Failure - Returns False or empty string
; Author ........:  Colduction (Ho3ein)
; Modified.......:
; Remarks .......: Function names should be written as string (inside of two Double Quotation or Quotation) to be executed and be measured
; Example .......; _FuncSpeedComparator('ConsoleWrite("10101010101")', 'ConsoleWrite("Hello World!")', 500, 3)
; ===============================================================================================================================

Func _FuncSpeedComparator($s1stFunc = "", $s2ndFunc = "", $iCallNum = 100, $iResultType = 1)
   If Not StringRegExp($s1stFunc, "^[a-zA-Z0-9_]+\x28(.*?)\x29$") Or Not StringRegExp($s2ndFunc, "^[a-zA-Z0-9_]+\x28(.*?)\x29$") Or Not StringRegExp($iCallNum, "^\p{Nd}*$") Or Not StringRegExp($iResultType, "^\p{Nd}*$") Then ; Human mistake preventative stage.
      Return False
   Else
   ; Measure stage.
      ;; First function measurement.
      Local $hTimer_1stFunc = TimerInit()
      For $i = 1 To $iCallNum
         Execute($s1stFunc)
      Next
      Local $iDiff_1stFunc = TimerDiff($hTimer_1stFunc)
      ;; Second function measurement.
      Local $hTimer_2ndFunc = TimerInit()
      For $i = 1 To $iCallNum
         Execute($s2ndFunc)
      Next
      Local $iDiff_2ndFunc = TimerDiff($hTimer_2ndFunc)
   ; Fastest function detector stage.
      Local $sFastestFunc = ""
      If $iDiff_1stFunc = $iDiff_2ndFunc Then
         $sFastestFunc = "Both of them"
      ElseIf $iDiff_1stFunc < $iDiff_2ndFunc Then
         $sFastestFunc = StringRegExpReplace($s1stFunc, "(\x28).*", "")
      Else
         $sFastestFunc = StringRegExpReplace($s2ndFunc, "(\x28).*", "")
      EndIf
   ; Results stage.
      Local $sResultText = @CRLF & '#Fastest Function: "' & $sFastestFunc & '"' & @CRLF & @CRLF & '1) "' & StringRegExpReplace($s1stFunc, "(\x28).*", "") & '" time elapsed: (' & $iDiff_1stFunc & ") ms" & @CRLF & '2) "' & StringRegExpReplace($s2ndFunc, "(\x28).*", "") & '" time elapsed: ' & "(" & $iDiff_2ndFunc & ") ms" & @CRLF
      If $iResultType = 1 Or Not StringRegExp($iResultType, '^[1|2|3]{1}$') Then ; Output as ConsoleWrite.
         ConsoleWrite($sResultText)
      ElseIf $iResultType = 2 Then ; Output as MsgBox.
         MsgBox(64, "Result: " & $sFastestFunc, $sResultText)
      ElseIf $iResultType = 3 Then ; Output as both ConsoleWrite & MsgBox.
         ConsoleWrite($sResultText)
         MsgBox(64, "Result: " & $sFastestFunc, $sResultText)
      EndIf
   EndIf
EndFunc   ;==>_FuncSpeedComparator

 

_FuncSpeedComparator.au3

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

×
×
  • Create New...