Jump to content
Sign in to follow this  
BOUNCER

how to make this more efficient

Recommended Posts

BOUNCER

$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

Share this post


Link to post
Share on other sites
darkjohn20

$Scan = 0

For $X = 1 To 10
    $Scan += StringInStr($dumpstring, "XXXX")
Next

If $Scan <> 0 Then
    
EndIf

Something like this perhaps?

Edited by darkjohn20

Share this post


Link to post
Share on other sites
BOUNCER

I dont get it...

where would i add more StringInStr($dumpstring, "XXXX")

Share this post


Link to post
Share on other sites
darkjohn20

It automatically loops that 10 times, and adds to the total value of $Scan.

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


Link to post
Share on other sites
Bowmore

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

Share this post


Link to post
Share on other sites
Malkey

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.

Share this post


Link to post
Share on other sites
Tvern

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.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.