0

if I am carrying out actions such as these on a classic ASP page:

if instr(tmp, "®") then tmp = replace(tmp, "®", "®")
if instr(tmp, "™") then tmp = replace(tmp, "™", "™")
if instr(tmp, "©") then tmp = replace(tmp, "©", "©")

is the replace function actually already doing an internal instr anyhow?? If so should the above code simply be changed to:

tmp = replace(tmp, "®", "®")
tmp = replace(tmp, "™", "™")
tmp = replace(tmp, "©", "©")

What is more resource efficient? They are being used in all sorts of scenarios such as being wrapped around user content in SQL statements etc...

Mat41
  • 1,287
  • 4
  • 15
  • 29
  • 1
    @lankymart thank you for your time and advice. Sometimes life is just not that simple. I have a complex situation for a very large global organisation. The DB is massive and the number of systems and processes that interoperate with it are high and largely undocumented. We are looking for all the avenues in the array of systems that insert data and will fix these underlying issues. In the mean time we have to deal with what we have in front of us and what we see. IMO my question is not only valid but useful to others. Yours and other good peoples advice is appreciated. – Mat41 Jul 11 '14 at 10:18
  • So you have a large database that has been inputting corrupt data for years? – user692942 Jul 11 '14 at 10:26
  • Yes thats correct. I am new to the business, these problems have been around long before me. There are asp pages, vbs jobs, batch jobs, data coming from other systems such as SAP, and more. Not to mention that its multi language. IMO you suggestion to 'drop this code' is very negative (not to mention wrong in this case). If I sat around and winged about the issue nothing would get done. The data must be replaced. – Mat41 Jul 11 '14 at 22:02
  • Your missing the point you've now just mentioned it's multi lingual but it's storing corrupted data which will not suit internationalization, if it can't handle simple stuff like `©` how do you expect to handle more challenging character sets? I've worked with multi-language classic asp applications for years now and my advice is to fix the encoding issues before you continue, otherwise you are leaving yourself open to a world of pain. You may see it as me being negative but honestly your doing more harm then good. `Replace()` to fix incorrectly encoded multi byte characters is not the answer. – user692942 Jul 11 '14 at 23:35
  • For anyone following this it all relates to the OPs original question which this is part of - http://stackoverflow.com/q/24666723/692942 – user692942 Jul 11 '14 at 23:37
  • To be honest replacing the characters that are already inside the database seems like the obvious way to deal with these. I am doing this with stored procs. One proc to find a certain character and them another to find and replace. Of course this is done after dealing with the user interfaces that are responsible for inserting this data. – Mat41 Jul 14 '14 at 02:57
  • I guess you don't know much about character encoding, oh well good luck. You'll need it. – user692942 Jul 14 '14 at 07:28
  • You really dont have any idea about my experience and what I know. Your attitude and comments are very interesting. All the best and good luck with your communication... – Mat41 Jul 14 '14 at 11:20
  • I know enough to make an educated guess, as fixing the encoding is not an option but creating a mismatch of encoding and corrupted data in your database is. You maybe a very good web developer but you'll be surprised how many don't know the basics of character encoding and the pitfalls of using methods like `Replace()` as a quick fix. I have tried to point these out to you, but it's your choice I can only advise. – user692942 Jul 14 '14 at 11:26

1 Answers1

3

With performance questions, the best answer is usually to benchmark. Fortunately, this situation is relatively easy to test:

Option Explicit

Const testString = "Some moderately long text string. Since I don't know long the actual input is, I'm just going to write some stuff. Oh, look, it's a special character! -> ®"

' Might want to start off with a lower number like 1000 and go up from there
Const numberOfIterations = 1000000

Dim replaceTime, inStrReplaceTime
Dim notReplaceTime, notInStrReplaceTime

' When search string is found in the target string
replaceTime = TestReplace("®")
inStrReplaceTime = TestInStrReplace("®")

' When search string is NOT found in the target string
notReplaceTime = TestReplace("©")
notInStrReplaceTime = TestInStrReplace("©")

WScript.Echo "Results (seconds, lower is better):" & vbNewline & _
    "  Replace: " & replaceTime & vbNewline & _
    "  InStr + Replace: " & inStrReplaceTime & vbNewline & _
    "  Replace (not in string): " & notReplaceTime & vbNewline & _
    "  InStr + Replace (not in string): " & notInStrReplaceTime & vbNewline

Function TestReplace(str)
    Dim startTime, i, outString

    startTime = Timer

    For i = 1 To numberOfIterations
        outString = Replace(testString, str, "something")
    Next

    TestReplace = Timer - startTime
End Function

Function TestInStrReplace(str)
    Dim startTime, i, outString

    startTime = Timer

    For i = 1 To numberOfIterations
        If InStr(testString, str) <> 0 Then outString = Replace(testString, str, "something")
    Next

    TestInStrReplace = Timer - startTime
End Function

On my machine, this script gave the output:

Results (seconds, lower is better):
  Replace: 0.8515625
  InStr + Replace: 1.234375
  Replace (not in string): 0.6796875
  InStr + Replace (not in string): 0.3046875

This may not be the most comprehensive test, but it does appear that the answer to which method is faster depends on whether or not you expect to see your search string in the string you are replacing.

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
  • Seems like a very good test to me. Awfully kind of you to give me your time and advice, thank you. It did seem logical to me that dropping the instr would be faster. A fine day to you – Mat41 Jul 11 '14 at 10:22
  • Is this not obvious? `Replace()` will only ever replace if it finds a match so adding an extra `InStr()` check is always going to be slower because it's an extra call. – user692942 Jul 11 '14 at 10:52
  • To be honest no its not obvious. I can make some kind of assumption however how do you know a replace function does not carry out some kind of instr process internally? Perhaps you know a way to literally see under the hood of functions in the VBScript world to identify things such as this. If so please share? – Mat41 Jul 14 '14 at 02:53
  • @Mat41 What does it matter?, your so hung up on this but it makes no difference. I don't know if `Replace()` does use `InStr()` or not internally but I do know what each functions job is. That is why its obvious. – user692942 Jul 14 '14 at 07:34