Jump to content

how to make this more efficient


Recommended Posts

$scan=StringInStr($dumpstring, "XXXX")
$scan2=StringInStr($dumpstring, "XXXX")
$scan3=StringInStr($dumpstring, "XXXX")
$scan4=StringInStr($dumpstring, "XXXX")
$scan5=StringInStr($dumpstring, "XXXX")
$scan6=StringInStr($dumpstring, "XXXX")

then i have

If $scan OR $scan2 OR $scan3 OR $scan4 OR $scan5 OR $scan6 <> 0 Then

this isnt very efficient how else can i do it

Link to comment
Share on other sites

Here is another possibility.

Local $dumpstring = "1224 qwer asdf zxcv9876xxxy"

; "|" means "or"
; "(xxxx)|(zasx)|" means match "xxxx" group of characters, or, "zasx" group of characters, or, etc.
If StringRegExp($dumpstring, "(xxxx)|(zasx)|(asdc)|(fdsa)|(hjk )|(erty)|(qwert)|(8769)") = 1 Then
    MsgBox(0, "", "Match", 3)
Else
    MsgBox(0, "", "No Match", 3)
EndIf
Link to comment
Share on other sites

It depends what you mean by "more efficient"? 

Your original code will be the fastest and the easiest to understand and debug. I often see post on this forum where the members try to write a section of code in fewer lines in the belief that fewer lines is always better and faster. This is not true. In general a task written using 10 simple lines of code will be better that 1 very complex line of code.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Local $dumpstring = "1224 qwer asdf zxcv9876xxxy"

Local $begin2 = TimerInit()
For $X = 1 To 10000
    $scan = StringInStr($dumpstring, "XXXX")
    $scan2 = StringInStr($dumpstring, "zasx")
    $scan3 = StringInStr($dumpstring, "asdc")
    $scan4 = StringInStr($dumpstring, "fdsa")
    $scan5 = StringInStr($dumpstring, "qwert")
    $scan6 = StringInStr($dumpstring, "8769")
    If $scan Or $scan2 Or $scan3 Or $scan4 Or $scan5 Or $scan6 <> 0 Then ConsoleWrite("" & @CRLF)
Next
ConsoleWrite("StringInStr Time " & TimerDiff($begin2) & @CRLF)

Local $begin = TimerInit()
For $X = 1 To 10000
    If StringInStr($dumpstring, "XXXX") Or _
            StringInStr($dumpstring, "zasx") Or _
            StringInStr($dumpstring, "asdc") Or _
            StringInStr($dumpstring, "fdsa") Or _
            StringInStr($dumpstring, "qwert") Or _
            StringInStr($dumpstring, "8769") Then ConsoleWrite("" & @CRLF)
Next
ConsoleWrite("If StringInStr Time " & TimerDiff($begin) & @CRLF)

Local $begin1 = TimerInit()
For $X = 1 To 10000
    ; "|" means "or"
    ; "(xxxx)|(zasx)|" means match "xxxx" group of characters, or, "zasx" group of characters, or, etc.
    If StringRegExp($dumpstring, "(xxxx)|(zasx)|(asdc)|(fdsa)|(qwert)|(8769)") Then ConsoleWrite("" & @CRLF)
Next

ConsoleWrite("StringRegExp Time " & TimerDiff($begin1) & @CRLF)

My results:-

StringInStr Time 922.701775666972

If StringInStr Time 717.410498615306

StringRegExp Time 573.851553238681

My results show that the single StringRegExp command is faster than the equivalent six StringInStr commands. Thus more efficient speed wise.

Have not check speeds when searching in a megabyte string.

Link to comment
Share on other sites

I did about the same test yesterday and came to the same conclusion, but I also noticed that the order in which you call the tests matters.

If I run the regex method first it's about 15% slower compared to running it last, so for an accurate comparison you'd have to run them in seperate scripts.

Either way the regexp has a good speed margin on the other methods.

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...