As to the problem, in general, note that it degrades to a repetetive task of searching for substrings within some longer string (needle in a haystack). There are a number of things one might do to optimize speed. Generally speaking, one locates a point where the bulk of the time is being spent and optimizes that area. Optimization is complicated with modern microprocessor architectures; the task is often best accomplished by the compiler, which considers (usually) these issues. Nonetheless, some thought on the part of the coder can be effective. If one looks at a well written string compare library function, one will find that it drops into assembly language for speed, and that it tries to locate a boundary for a larger data type than a byte. Comparing 4 bytes with one test is obviously faster than comparing the 4 bytes one at a time. Even if one doesn't care to expend that amount of effort because the problem is a one-off that won't be used in copius quantities, one can still improve the performance beyond what first flows from the fingertips.
The following is a clip from another post. I'm not linking to it, as it was on a competing forum.
Quote:
|
for some reason, there seems to be no function that searches for a substring within another string using CASELESS comparison. i need a fucntion like this for the heart of a proxy filtering scheme. every reply is searched for a slew of different tokens, this function would be called each time. this function is absolutely critical to serving timely client requests - it is called repeatedly constantly. i've written several different versions, this is the best i can come up with so far. anything better??
|
I will produce the OP's code and my modification in a couple of days. The upshot is that the improved code (which didn't jump through all the hoops mentioned above) resulted in the following (First Sub is the OP's code, Second Sub is the modified code):
Quote:
Output:
------------------------------------------------------------------------------
Functionality test
String: Now iS tHe TiMe, iS tHe PlAcE, iS tHe WaY
Substring: Is ThE tImE
Func1 return: iS tHe TiMe, iS tHe PlAcE, iS tHe WaY
Func2 return: iS tHe TiMe, iS tHe PlAcE, iS tHe WaY
String: Now iS tHe TiMe, iS tHe PlAcE, iS tHe WaY
Substring: Is ThE wA
Func1 return: iS tHe WaY
Func2 return: iS tHe WaY
String: Now iS tHe TiMe, iS tHe PlAcE, iS tHe WaY
Substring: Is ThE wAy
Func1 return: (null) <<--------------------- ! ! ! ! !
Func2 return: iS tHe WaY
Execution time test...
First subs return and clocks: iS tHe WaY, 953
Second subs return and clocks: iS tHe WaY, 422
|
The functionality test also reveal a failure in the OP's code under certain conditions (Functionality test #3, above).