Kale wrote:
Sorry, but c'mon, we're talking a few cycles here, lets be realistic.
I'm not saying your routine is slow or bad for the solution of the problem.
If you have to call it only some times in your code the time spent is immaterial in both the implementations. Who cares ?
But you can't say to someone is anal, is overoptimizing etc, only because his code is faster, and with really a small effort. The second implementation is better and faster, it's a fact.
But not only a little faster. It's twice the speed.
Again I don't see nothing wrong in suggest a better code. It's a programmers' forum.
Anyone in his programs can make the most opportune choices later !
Immaterial for 5, even 100 calls ? I agree.
Bad coded ? Anal ? Overoptimized ? Not to me.
Deserving mockery ? I don't think so.
The point where you were right in objecting was the "memory leak" affermation.
The PB manual says a object recreated with the same id free the previous one.
Code: Select all
Procedure.s RemoveExtension_1 (Filename.s)
CreateRegularExpression(0, "(?<!\A)\.[\w]+\Z")
ProcedureReturn ReplaceRegularExpression(0, Filename, "")
EndProcedure
Procedure.s RemoveExtension_2 (sFilename.s)
Static idRegEx.i
; Object recycling.
If Not (idRegEx)
idRegEx = CreateRegularExpression(#PB_Any , "(?<!\A)\.[\w]+\Z")
If Not (idRegEx)
ProcedureReturn sFilename
EndIf
EndIf
ProcedureReturn ReplaceRegularExpression(idRegEx, sFilename, "")
EndProcedure
#cycles = 1000000
sTest.s = "c:\temp\filename.txt"
iStartTime = ElapsedMilliseconds()
For k = 1 To #cycles
RemoveExtension_1 (sTest)
Next
iEndTime = ElapsedMilliseconds()
MessageRequester("", Str(iEndTime - iStartTime))
iStartTime = ElapsedMilliseconds()
For k = 1 To #cycles
RemoveExtension_2 (sTest)
Next
iEndTime = ElapsedMilliseconds()
MessageRequester("", Str(iEndTime - iStartTime))
Kale wrote:luis wrote:...it's certainly safer.
In what way?
I explained in the post above...:
You are using 0 as id for the regexp. Using PB_Any is safer because you don't risk to call your procedure inside some other code using too a regexp potentially with the same id (0).
Doing so the inner CreateRegularExpression() (your proc) would destroy the outer one.
So this way is safer too. This is a lot more important than the talk about the speed to me.
Code: Select all
Procedure.s RemoveExtension_1 (Filename.s)
CreateRegularExpression(0, "(?<!\A)\.[\w]+\Z")
ProcedureReturn ReplaceRegularExpression(0, Filename, "")
EndProcedure
Procedure.s RemoveExtension_2 (sFilename.s)
Static idRegEx.i
; Object recycling.
If Not (idRegEx)
idRegEx = CreateRegularExpression(#PB_Any , "(?<!\A)\.[\w]+\Z")
If Not (idRegEx)
ProcedureReturn sFilename
EndIf
EndIf
ProcedureReturn ReplaceRegularExpression(idRegEx, sFilename, "")
EndProcedure
#cycles = 10
sTest.s = "c:\temp\filename.txt"
iStartTime = ElapsedMilliseconds()
For k = 1 To #cycles
CreateRegularExpression(0, "hello")
; some other code
; call the Kale's routine ... was that using a regexp ? I don't remember
; certainly I don't go to check every routine to see if it's destroying
; the environment of the caller
; every routine is closed right ? well tested right ? right !
Debug RemoveExtension_1 (sTest)
; ok now I will try to see if match
Debug MatchRegularExpression(0, "hello world")
; what ? does not!
Next
Change RemoveExtension_1 with RemoveExtension_2 and all is ok again.