Sign in to follow this  
Followers 0

_IsValidFileType()

76 posts in this topic

Posted (edited)

I took the idea from the function>_RFLTA_ListToMask() by Melba23 and modified it a little. So all the credit should be focused towards those mentioned in the function header.

Any suggestions or problems post below.

Function:

#include <StringConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _IsValidFileType
; Description ...: Checks if a filepath contains an approved filetype extension.
; Syntax ........: _IsValidFileType($sFilePath[, $sList = 'bat;cmd;exe'[, $bOpFast = True]])
; Parameters ....: $sFilePath           - A filepath.
;                  $sList               - [optional] A list of filetype extensions separated with ';' e.g. 'zip;rar;7zip;gzip'. Default is 'bat;cmd;exe'.
;                  $bOpFast             - [optional] True, use faster comparison; otherwise false, use standard comparison. Default is True.
; Return values .: Success - True
;				   Failure - False
; Author ........: guinness
; Modified ......:
; Remarks........; The code was initially based on the original function of _RFLTA_ListToMask() by Melba23, but overtime with input from
;                  AZJIO, Beege and Shaggi, I opted on using Robjong's suggestion [http://www.autoitscript.com/forum/topic/123674-isvalidtype/page__view__findpost__p__959470.]
; Related .......: _WinAPI_PathMatchSpec() in WinAPIEx.au3 by Yashied.
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _IsValidFileType($sFilePath, $sList = 'bat;cmd;exe', $bOpFast = True)
	Return StringRegExp($sFilePath, '\.(?i:\Q' & StringReplace($sList, ';', '\E|\Q', 0, (($bOpFast Or $bOpFast = Default) ? $STR_NOCASESENSEBASIC : $STR_NOCASESENSE)) & '\E)\z') > 0
EndFunc   ;==>_IsValidFileType
Example 1:

ConsoleWrite(_IsValidFileType('AutoIt3.cmd') & @CRLF) ; Returns True as .cmd has been approved as a valid filetype.
ConsoleWrite(_IsValidFileType('C:\AutoIt3.exe') & @CRLF) ; Returns True as .exe has been approved as a valid filetype.
ConsoleWrite(_IsValidFileType('C:\AutoIt3.zip') & @CRLF) ; Returns False as .zip hasn't been approved as a valid filetype.
ConsoleWrite(_IsValidFileType('C:\AutoIt3.zip', 'zip;rar;7zip;gzip') & @CRLF) ; Returns True as .zip has been approved as a valid filetype.
Example 2:

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

Example()

Func Example()
	Local $hGUI = GUICreate('_IsValidFileType Simple Drop', 255, 155, Default, Default, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES)
	Local $iLabel = GUICtrlCreateLabel('Drop Supported FileType (au3/exe/txt)', 2, 2, 250, 150, $SS_CENTER)
	GUICtrlSetState(-1, $GUI_DROPACCEPTED)
	GUISetState(@SW_SHOW, $hGUI)

	While 1
		Switch GUIGetMsg()
			Case $GUI_EVENT_CLOSE
				ExitLoop

			Case $GUI_EVENT_DROPPED
				If _IsValidFileType(@GUI_DragFile, 'au3;exe;txt') Then
					GUICtrlSetData($iLabel, 'Dropped - ' & @GUI_DragFile)
				Else
					GUICtrlSetData($iLabel, 'I said ''Supported FileType!''')
				EndIf

		EndSwitch
	WEnd
	GUIDelete($hGUI)
EndFunc   ;==>Example
For a more complex approach with an improvement in speed then have a look at Robjong's example in >post #54. It should be noted that the speed difference isn't in seconds but milli/nanoseconds. Edited by guinness

Share this post


Link to post
Share on other sites



Posted

guinness,

The hours were not spent just by me - I had a lot of help! :x

The function header says it all:

"SRE patterns developed from those posted by various forum members and Spiff59 in particular"

M23

Share this post


Link to post
Share on other sites

Posted

OK, sorry about that. I updated the OP to reflect the countless hours of improvement :x

Share this post


Link to post
Share on other sites

Posted

Update OP and added a new Example.

Share this post


Link to post
Share on other sites

Posted

I updated the OP with a small bug fix, which was fixed in Melba's latest

Share this post


Link to post
Share on other sites

Posted

why all the regexp?

func isvalidtype($path, $aftypes = 0)
	if not $aftypes or not isarray($aftypes) then
		local $types[4] = ["pif","bat","exe","cmd"]
	else
		local $types = $aftypes
	endif
	local $ipos = stringinstr($path,".",default,-1), $stype
	if not $ipos then return false
	$stype = stringmid($path,$ipos+1)
	for $ext in $types
		if $stype = $ext then return true
	next
	return false
endfunc

Share this post


Link to post
Share on other sites

Posted (edited)

Sorry? What's the problem with using SRE?

Edit: Also your function is badly formatted and will not pass Au3Check.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Sorry? What's the problem with using SRE?

besides being a lot slower, i think its overly complicated for such a small task. but most importantly, it embeds another language which most of autoit users dont understand and thus obfuscates the code. (kiss)

Edit: Also your function is badly formatted and will not pass Au3Check.

? it does. i dont see anything unusual besides lower case

Share this post


Link to post
Share on other sites

Posted

? it does. i dont see anything unusual besides lower case

Run Au3Check and you'll see why. Hint: Declaring a variable twice.

besides being a lot slower, i think its overly complicated for such a small task. but most importantly, it embeds another language which most of autoit users dont understand and thus obfuscates the code. (kiss)

No comment as I want to keep this thread on topic.

Share this post


Link to post
Share on other sites

Posted

Run Au3Check and you'll see why. Hint: Declaring a variable twice.

>Running AU3Check (1.54.19.0) from:C:\Program Files (x86)\AutoIt3

+>00:00:04 AU3Check ended.rc:0

you might have another version? but no variable is declared twice.

No comment as I want to keep this thread on topic.

cool, but you asked yourself :) - anyway, it was just an honest comment

Share this post


Link to post
Share on other sites

Posted

if not $aftypes or not isarray($aftypes) then

local $types[4] = ["pif","bat","exe","cmd"]

else

local $types = $aftypes

Apologies Au3Check didn't show an error.

Share this post


Link to post
Share on other sites

Posted

If to execute StringReplace($sList, ";", "|*."), symbol ";" any more won't be. The next line isn't meaningful

"s*;s*", ";"

";", "|"

-------------------

"|/|:|<|>||" = [/:<>|]

Share this post


Link to post
Share on other sites

Posted

Good catch AZJIO thanks! And also for the other advice.

Share this post


Link to post
Share on other sites

Posted

I've updated the original post with a mistake pointed out by AZJIO.

Share this post


Link to post
Share on other sites

Posted

Actually a lot can be improved, give me 10 minutes.

Share this post


Link to post
Share on other sites

Posted

Complete. I stripped the function back to the very basics. I've updated the original post.

Share this post


Link to post
Share on other sites

Posted

Shaggi,

Maybe I'm wrong but _IsValidType seems a little quicker now.

Local $iTimer = TimerInit()
For $A = 1 To 5000
	isvalidtype(@ScriptFullPath)
Next
ConsoleWrite(TimerDiff($iTimer) / 5000 & @LF)

$iTimer = TimerInit()
For $A = 1 To 5000
	_IsValidType(@ScriptFullPath)
Next
ConsoleWrite(TimerDiff($iTimer) / 5000 & @LF)

Func isvalidtype($path, $aftypes = 0)
	If Not $aftypes Or Not IsArray($aftypes) Then
		Local $types[3] = ["bat", "exe", "cmd"]
	Else
		Local $types = $aftypes
	EndIf
	Local $ipos = StringInStr($path, ".", Default, -1), $stype
	If Not $ipos Then Return False
	$stype = StringMid($path, $ipos + 1)
	For $ext In $types
		If $stype = $ext Then Return True
	Next
	Return False
EndFunc   ;==>isvalidtype

Func _IsValidType($sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[/:<>|]") Then
		Return SetError(1, 0, -1)
	EndIf
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?." & StringReplace($sList, ";", "|.*?.") & ")z")
EndFunc   ;==>_IsValidType

Share this post


Link to post
Share on other sites

Posted

Shaggi,

Maybe I'm wrong but _IsValidType seems a little quicker now.

Local $iTimer = TimerInit()
For $A = 1 To 5000
	isvalidtype(@ScriptFullPath)
Next
ConsoleWrite(TimerDiff($iTimer) / 5000 & @LF)

$iTimer = TimerInit()
For $A = 1 To 5000
	_IsValidType(@ScriptFullPath)
Next
ConsoleWrite(TimerDiff($iTimer) / 5000 & @LF)

Func isvalidtype($path, $aftypes = 0)
	If Not $aftypes Or Not IsArray($aftypes) Then
		Local $types[3] = ["bat", "exe", "cmd"]
	Else
		Local $types = $aftypes
	EndIf
	Local $ipos = StringInStr($path, ".", Default, -1), $stype
	If Not $ipos Then Return False
	$stype = StringMid($path, $ipos + 1)
	For $ext In $types
		If $stype = $ext Then Return True
	Next
	Return False
EndFunc   ;==>isvalidtype

Func _IsValidType($sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[\\/:<>|]") Then
		Return SetError(1, 0, -1)
	EndIf
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?\." & StringReplace($sList, ";", "|.*?\.") & ")\z")
EndFunc   ;==>_IsValidType

perhaps, i dont have the old function anymore? anyway, to test the two, here's a little script that show the difference:

SmallTest()
ScaledTest()

Func SmallTest()
	$types = "cmd;"
	$etypes = stringsplit($types,";",2)
	$path = "C:\"
	$ext = "wanted_file.err" ;extension is not in the list, so both algorithms have to search everything through
	$path &= $ext

	;Calculate the overhead for the loop and a lot of func calls
	$testtime = timerinit()
	for $i = 0 to 0xFFFF
		testfunc($path,$types)
	Next
	$overhead = timerdiff($testtime)
	ConsoleWrite("Assumed overhead: "& $overhead & @lf)

	;test of new _isvalidtype
	$starttime = timerinit()
	for $i = 0 to 0xFFFF
		_IsValidType($path,$etypes)
	next
	$test1 = timerdiff($starttime)

	;test of isvalidtype
	$starttime = timerinit()
	for $i = 0 to 0xFFFF
		isvalidtype($path,$types)
	next
	$test2 = timerdiff($starttime)

	ConsoleWrite("(small) Test data: " & @lf & "_isvalidtype: " & $test1 - $overhead & @lf & "isvalidtype: " & $test2 - $overhead & @lf)
endfunc

Func ScaledTest()
	$types = "cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;"
	$etypes = stringsplit($types,";",2)
	$path = "C:\random1\random2\random3\random4\random5\random6\random7\random8\random9\random10\random11\random12\random13\random14\random15\random16\random17\random18\random19\random20\random21\random22\random23\random24\random25\random26\random27\random28\random29\random30\"
	$ext = "wanted_file.err" ;extension is not in the list, so both algorithms have to search everything through
	$path &= $ext

	;Calculate the overhead for the loop and a lot of func calls
	$testtime = timerinit()
	for $i = 0 to 0xFFFF
		testfunc($path,$etypes)
	Next
	$overhead = timerdiff($testtime)
	ConsoleWrite("Assumed overhead: "& $overhead & @lf)

	;test of new _isvalidtype
	$starttime = timerinit()
	for $i = 0 to 0xFFFF
		_IsValidType($path,$types)
	next
	$test1 = timerdiff($starttime)

	;test of isvalidtype
	$starttime = timerinit()
	for $i = 0 to 0xFFFF
		isvalidtype($path,$etypes)
	next
	$test2 = timerdiff($starttime)

	ConsoleWrite("(scaled) Test data: " & @lf & "_isvalidtype: " & $test1 - $overhead & @lf & "isvalidtype: " & $test2 - $overhead & @lf)
EndFunc




func isvalidtype(byref $path, $aftypes = 0)
	if not $aftypes or not isarray($aftypes) then
		local $types[3] = ["cmd","bat","exe"]
	else
		local $types = $aftypes
	endif
	local $ipos = stringinstr($path,".",0,-1), $stype
	if not $ipos then return false
	$stype = stringmid($path,$ipos+1)
	for $ext in $types
		if $ext = $stype then
			return true
		endif
	next
	return false
endfunc

Func _IsValidType($sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[\\/:<>|]") Then
		Return SetError(1, 0, -1)
	EndIf
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?\." & StringReplace($sList, ";", "|.*?\.") & ")\z")
EndFunc   ;==>_IsValidType

Func testfunc($a,$b)
EndFunc

results here:

Assumed overhead: 212.181195527824

(small) Test data:

_isvalidtype: 1261.08944816422

isvalidtype: 2009.5421428352

Assumed overhead: 219.468354359682

(scaled) Test data:

_isvalidtype: 25379.333455585

isvalidtype: 1597.608925734

What the test shows, is that yours excels in very small tests, however as the test size grows to medium-long filenames and types, it becomes 16x times slower.

this is because my function is designed to take near constant (actually less in test - but dont count on that) time O(1) for any request , because it searches backwards in the input string, and the only real overhead is the iteration and comparison of the array.

Yours however, seems to be more like linear time O(n), which is not bad, but if this algorithm is used in a recursively folder search with file extension masking, you will note the difference.

Share this post


Link to post
Share on other sites

Posted

OK fair enough. Thanks for testing.

Share this post


Link to post
Share on other sites

Posted

besides being a lot slower

What? Calling SRE a lot slower is fucking crazy. Theres a good reason pcre is included in a ton of languages out there. Its powerful and fast.

but most importantly, it embeds another language which most of autoit users dont understand and thus obfuscates the code.

Don't use the tools built into autoit because some people might not understand how they work? Thats weak. Especially coming from someone whos using hex values in there For loops.

I also want to point out in that perfomance test, the main slowdown is coming from StringReplace(). Not SRE. If you change the code to

Func _IsValidType(ByRef $sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[\\/:<>|]") Then Return SetError(1, 0, -1)
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?\." & StringReplace($sList, ";", "|.*?\.", 2) & ")\z")
EndFunc   ;==>_IsValidType

my results then become:

(scaled) Test data:
_isvalidtype: 3263.63947284885
isvalidtype: 1521.45284004219

Which is only 2x slower. And more so, in reality your function only gains micro seconds. In my opinion its not worth turning a 2 line function into 10 lines just to gain microseconds.

@@ Debug(62) : $test1 = 0.128203815394698
@@ Debug(70) : $test2 = 0.0692218683760509

Share this post


Link to post
Share on other sites

Posted (edited)

What? Calling SRE a lot slower is fucking crazy. Theres a good reason pcre is included in a ton of languages out there. Its powerful and fast.

A lot slower in the context, dude. Chill off. No matter how you twist and turn it, interpreting a small language like regexp is always going to be slower than a linear search for a dot. Which is why i mean that stringinstr is the right tool for the job. regexp has its use, ofcourse.

Don't use the tools built into autoit because some people might not understand how they work? Thats weak. Especially coming from someone whos using hex values in there For loops.

If you consider the launguage were using, and the job this function have to do, im sure you'll agree that it is overkill. 0xFFFF is (for me) a convenient way of writing 65535. I dont know what you find more "readable".

I also want to point out in that perfomance test, the main slowdown is coming from StringReplace(). Not SRE. If you change the code to
Func _IsValidType(ByRef $sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[/:<>|]") Then Return SetError(1, 0, -1)
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?." & StringReplace($sList, ";", "|.*?.", 2) & ")z")
EndFunc   ;==>_IsValidType
my results then become:
(scaled) Test data:
_isvalidtype: 3263.63947284885
isvalidtype: 1521.45284004219[/quote]
Good find 
[quote]Which is only 2x slower. And more so, in reality your function only gains micro seconds. In my opinion its not worth turning a 2 line function into 10 lines just to gain microseconds. [code='Single times']@@ Debug(62) : $test1 = 0.128203815394698
@@ Debug(70) : $test2 = 0.0692218683760509

You can optimize my function a lot more if you want, like you did with guiness's. if you use 69255's function, numbers turn out like this:

Assumed overhead: 255.780810900625

(small) Test data:

_isvalidtype: 1324.87687182289

isvalidtype: 564.974722769614

Assumed overhead: 234.744067460558

(scaled) Test data:

_isvalidtype: 4596.83166602593

isvalidtype: 509.395533968111

Which is still around 10x faster. And if you worry about my functions length, you can shrink it to one line, as you saw. - but in both cases at the cost of readability and as of such more "bad style".

Using the right algorithm is for me programming 101. Getting the right understand of algorithm complexity can help you program much better code, and imo it would be ignorant not too. And still, if people did not do this, you're welcome to have your computer work at half the speed (or ten times slower).

Edited by Shaggi

Share this post


Link to post
Share on other sites

Posted

I also want to point out in that performance test, the main slowdown is coming from StringReplace(). Not SRE. If you change the code to

Nice idea, I suppose a third parameter could be added for the number of extensions (minus 1) that are being checked.

Share this post


Link to post
Share on other sites

Posted (edited)

Hi,

I came across this topic and I think the function can be optimized quite a bit.

The caret (^) has no real function, there is no need to return if any regex meta characters are present in the 'list' var and the way extensions are prepended with '|.*?.' makes for a very exhaustive pattern, because for every entry in the group costly matching needs to be done.

Here is my take on it, we need to match the last dot followed by the extension, which will be in the 'list' matching group, and then the string end.

An extension in the 'list' var might contain regex meta characters and therefor needs to be escaped, instead of calling StringRegExpReplace to prepend meta characters with a backslash we will turn the regex engine of for each extension in the group with the E and Q modifiers.

Func _IsValidType($sFilePath, $sList = "bat;cmd;exe")
	Return StringRegExp($sFilePath, ".(?i:Q" & StringReplace($sList, ";", "E|Q") & "E)z")
EndFunc   ;==>_IsValidType

It is shorter then the original, it's also faster and it will match any characters (except for Q).

@Shaggi

The timing test you provided has one major flaw, and the results should be disregarded, here is why.

The function _IsValidType handles the 'list' var ($sList) in the function every time it is called, the isvalidtype function uses the array created by StringSplit which is done once before the loop. If you modify the isvalidtype function to accept the same string as the second param and call StringSplit inside the function at every call the SRE method is faster.

The test I used and the results:

#_isValidType - The new StringRegExp method
#old_isValidType - The old StringRegExp method
#old_isValidType - The old StringInStr method

(small) Test data:
Assumed overhead: 360.180545972701
_isValidType: 946.429958502075
old_IsValidType: 1588.35091745413
older_IsValidType: 2860.05594720264

(scaled) Test data:
Assumed overhead: 361.250002499875
_isValidType: 3605.51788910554
old_IsValidType: 22781.9302184891
older_IsValidType: 6104.03551822409

SmallTest()
ScaledTest()


Func SmallTest()
	$types = "cmd;"
	$path = "C:"
	$ext = "wanted_file.err" ;extension is not in the list, so both algorithms have to search everything through
	$path &= $ext

	;Calculate the overhead for the loop and a lot of func calls
	$testtime = TimerInit()
	For $i = 0 To 0xFFFF
		testfunc($path, $types)
	Next
	$overhead = TimerDiff($testtime)

	;test of older_IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		older_IsValidType($path, $types)
	Next
	$test1 = TimerDiff($starttime)

	;test of old_IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		old_IsValidType($path, $types)
	Next
	$test2 = TimerDiff($starttime)

	;test of _IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		_IsValidType($path, $types)
	Next
	$test3 = TimerDiff($starttime)

	ConsoleWrite("(small) Test data: " & @CRLF & _
			"Assumed overhead: " & $overhead & @CRLF & _
			"_isValidType: " & $test3 - $overhead & @CRLF & _
			"old_IsValidType: " & $test2 - $overhead & @CRLF & _
			"older_IsValidType: " & $test1 - $overhead & @CRLF)

EndFunc   ;==>SmallTest

Func ScaledTest()
	$types = "cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;"
	$path = "C:random1random2random3random4random5random6random7random8random9random10random11random12random13random14random15random16random17random18random19random20random21random22random23random24random25random26random27random28random29random30"
	$ext = "wanted_file.err" ;extension is not in the list, so both algorithms have to search everything through
	$path &= $ext

	;Calculate the overhead for the loop and a lot of func calls
	$testtime = TimerInit()
	For $i = 0 To 0xFFFF
		testfunc($path, $types)
	Next
	$overhead = TimerDiff($testtime)

	;test of older_IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		older_IsValidType($path, $types)
	Next
	$test1 = TimerDiff($starttime)

	;test of old_IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		old_IsValidType($path, $types)
	Next
	$test2 = TimerDiff($starttime)

	;test of _IsValidType
	$starttime = TimerInit()
	For $i = 0 To 0xFFFF
		_IsValidType($path, $types)
	Next
	$test3 = TimerDiff($starttime)

	ConsoleWrite("(scaled) Test data: " & @CRLF & _
			"Assumed overhead: " & $overhead & @CRLF & _
			"_isValidType: " & $test3 - $overhead & @CRLF & _
			"old_IsValidType: " & $test2 - $overhead & @CRLF & _
			"older_IsValidType: " & $test1 - $overhead & @CRLF)
EndFunc   ;==>ScaledTest

Func _IsValidType($sFilePath, $sList = "bat;cmd;exe")
	Return StringRegExp($sFilePath, ".(?i:Q" & StringReplace($sList, ";", "E|Q") & "E)z")
EndFunc   ;==>_IsValidType

Func old_IsValidType($sFilePath, $sList = "bat;cmd;exe")
	If StringRegExp($sList, "[/:<>|]") Then
		Return SetError(1, 0, -1)
	EndIf
	Return StringRegExp($sFilePath, "(?i)^(" & ".*?." & StringReplace($sList, ";", "|.*?.") & ")z")
EndFunc   ;==>old_IsValidType

Func older_IsValidType($path, $aftypes = "bat;cmd;exe")
	$etypes = StringSplit($aftypes, ";", 2)
	If Not IsArray($etypes) Or Not UBound($etypes) Then
		Local $types[3] = ["cmd", "bat", "exe"]
	Else
		Local $types = $etypes
	EndIf
	Local $ipos = StringInStr($path, ".", 0, -1), $stype
	If Not $ipos Then Return False
	$stype = StringMid($path, $ipos + 1)
	For $ext In $types
		If $ext = $stype Then
			Return True
		EndIf
	Next
	Return False
EndFunc   ;==>older_IsValidType

Func testfunc($a, $b)
EndFunc   ;==>testfunc

Edited by Robjong

Share this post


Link to post
Share on other sites

Posted (edited)

A lot slower in the context, dude. Chill off.

CHILL? CHILL? My middle name cool! ;)

But in all seriousness its not that I don't agree with some off the things your saying shaggi because I do. It was the way you presented it out. Guinness is a great coder with tons of his work laid all through out theses fourms and because of that lots people read, and learn from these threads. When they get to your first posts, they see you calling SRE slow, then some performance results making it to be 16x slow. They then take from that, WOW I'm never going to use SRE. So thats why it left a bad taste in my mouth. ;)

edit: And the SRE guru Robjong has arrived. welcome :)

Edited by Beege

Share this post


Link to post
Share on other sites

Posted

The timing test you provided has one major flaw, and the results should be disregarded, here is why.

The function _IsValidType handles the 'list' var ($sList) in the function every time it is called, the isvalidtype function uses the array created by StringSplit which is done once before the loop. If you modify the isvalidtype function to accept the same string as the second param and call StringSplit inside the function at every call the SRE method is faster.

Obviously, the interface is a bit different between the two functions. My functions loops an array for checks - the user provides the mask as an array, for performance of course, the other analyses a string for matches - the user provides a mask as a string as anything else would overly complicate the function (you would have to concate the array down to a string).

They are inherently not the same, and thus i dont think it would be fair to make input list in my function as a string, then convert it to an array (that would be idiotic), as it would be the other way around. Using an array for a list is preferred at all times in general, according to me. it doesn't make sense to keep a list in a string, and then split it everytime you need data from it imo, and therefore i dont think it disqualifies my function.

However, if you want to do that comparison, use 69255's function, as it looks more like the other (accepts a ; delimited string) - and is some 3-4 times faster than my original.

CHILL? CHILL? My middle name cool! ;)

But in all seriousness its not that I don't agree with some off the things your saying shaggi because I do. It was the way you presented it out. Guinness is a great coder with tons of his work laid all through out theses fourms and because of that lots people read, and learn from these threads. When they get to your first posts, they see you calling SRE slow, then some performance results making it to be 16x slow. They then take from that, WOW I'm never going to use SRE. So thats why it left a bad taste in my mouth. ;)

edit: And the SRE guru Robjong has arrived. welcome :)

Well i'm sorry, i obviously didn't know your middlename :) but when you start using bold text and swearwords (not that i dont though) defending something that is slower, well..

regexp is an advanced tool, and has many good uses, but i think that this example (derived from a recursive file search?) could benefit from added clearness and speed (why not?). And the topic invited for suggestions, so i posted one :D generally, the idom KISS (keep it simple, stupid!) should be followed at all times (i think, especially in public code / udf's).

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

  • Recently Browsing   0 members

    No registered users viewing this page.