开发者

in VBA/QTP how does string comparison work? Is there an efficient string hashing function built in?

I need to compare two very large strings in QTP (intepreted by a vba-derived engine, I believe). I know these strings will exceed 100,000 characters, and I need to be able to detect when there is a change in one of these large strings. To start off I used the following:

if prevtext <> currenttext then ... end if

I was expecting this to explode, but actually the script ran quite fast. There was no noticeable slowdown during the string comparison. So, I am suspicious that the string compare is actually 开发者_如何学JAVAtruncating the strings outside of my scope or doing something else that would be cheating. Does anyone know if I can actually rely on the built in string comparison operator to compare two 100,000+ character strings? If not is there some native hashing function that I can replace this with? It just needs to be able to detect changes, not the content of the changes, and it needs to run quickly.


VBA will allow about 2 billion characters: http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/a1d8ee65-75e6-4a29-9670-6e233c398642


String compares, in any sane language, run in linear time. A hashing algorithm will not improve the string comparison speed unless the strings are only updated very infrequently and hashed when they are updated (as opposed to hashed before comparison). This would require modifying your application to contain a structure of Strings and their associated hash.


It doesn't look like there's any optimization (the obvious optimization would be to check if the strings are the same length) however when comparing 1,000,000 char string it takes less than 0.1 seconds so I don't think you should worry about 100K char strings.

Here's how I tested it.

base = String(1000000, "x")
first = base & "a"
sameLength = base & "b"
differentLength = base & "cd"

MercuryTimers.Timer("x").Start
diff = first <> sameLength
print "sameLength = " & diff & " after " & MercuryTimers.Timer("x").ElapsedTime
MercuryTimers.Timer("x").Reset

MercuryTimers.Timer("x").Start
diff = first <> differentLength
print "differentLength = " & diff & " after " & MercuryTimers.Timer("x").ElapsedTime
MercuryTimers.Timer("x").Reset

The timed outputs were about the same, here's a typical output (time is in milliseconds)

sameLength = True after 40
differentLength = True after 30

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜