Hi all.
I have a string and need to extract various (sometimes overlapping) parts based solely on position, regardless of contents. Does anyone know the fastest possible way to do this, in VB6?
I have been considering whether I might even try building an assembler (DLL) routine, but would prefer not to have to - it has been quite a while since I played with assembler.
I have some code which makes lots of decisions based on various chunks of a string. If I were hitting the same position repeatedly, I would just extract it once to a variable and use that. But I have to keep checking different positions, based on records types. At the moment I'm taking the simple and obvious route of using Mid$ to access each chunk.
What I really probably comes under the heading of unobtainable perfection. In the programming language "Natural", you can "redefine" a field, which simply assigns another variable overlapping the specified portion. It's kind of like a Mid$ which is set up at design time. Absolutely wonderful for this kind of thing, as you don't need to do any sort of function call or calculation at runtime.
I'm doing this for millions upon millions of text records each time I run, so what I'm looking for is some way, any way, to improve on the performance of the Mid$ in chopping up the string.
3 1390 kadghar 1,295
Recognized Expert Top Contributor
I have some code which makes lots of decisions based on various chunks of a string. If I were hitting the same position repeatedly, I would just extract it once to a variable and use that. But I have to keep checking different positions, based on records types. At the moment I'm taking the simple and obvious route of using Mid$ to access each chunk.
I think thats the solution, extract each part of the string to an array and work with the array. It will take you a little time to get that array and it'll be much faster than working with Mid$
I made this little test and the results are below. - Sub compare()
-
Dim Str1 As String
-
Dim Str2 As String
-
Dim mStr1() As String
-
Dim Sin1 As Single
-
Dim Sin2 As Single
-
Dim Sin3 As Single
-
Dim i As Integer
-
Dim j As Long
-
Str1 = "Now i'll write here whatever it comes to my mind just to make this little test."
-
-
ReDim mStr1(1 To Len(Str1))
-
'Lets see how long it takes to save it into an array
-
Sin1 = Timer
-
For i = 1 To Len(Str1)
-
mStr1(i) = Mid(Str1, i, 1)
-
Next
-
Sin1 = Timer - Sin1
-
-
'Now lets write the string upside down 1'000'000 times
-
'and take the time using the old mid$
-
-
Sin2 = Timer
-
For j = 1 To 1000000
-
Str2 = ""
-
For i = Len(Str1) To 1 Step -1
-
Str2 = Str2 & Mid(Str1, i, 1)
-
Next
-
Next
-
Sin2 = Timer - Sin2
-
-
'and take the time using the array
-
-
Sin3 = Timer
-
For j = 1 To 1000000
-
Str2 = ""
-
For i = Len(Str1) To 1 Step -1
-
Str2 = Str2 & mStr1(i)
-
Next
-
Next
-
Sin3 = Timer - Sin3
-
-
MsgBox ("Array time: " & Sin1 & Chr(10) & "Mid$ Method: " & Sin2 & Chr(10) & "Array Method: " & Sin3)
-
-
End Sub
Sin1 = 0.0001875
Sin2 = 40.28319
Sin3 = 18.29994
This wont make your code much faster, but at least will reduce the time of your operations to one half.
Hope that helps.
(oh, and im in the job's computer, i think almost every other pc can make it faster.)
Thanks kadghar, I'll have a play with this at lunch time.
Hm... if an array of String is faster, an array of Byte might be even faster still.
However, in this case I suspect I may lose too much time copying to the array. You see, the problem is I'm not chopping up the same string very many times. I'm reading millions of records and doing this sort of general processing... (all values here are just made up for illustration purposes, I forget the actual details right now) - strRecordType = Mid$(strRecord, 5, 8)
-
Select Case strRecordType
-
Case TYPE_1 ' (a Const value)
-
strDetails1 = Mid$(strRecord, 18, 15)
-
strDetails2 = Mid$(strRecord, 56, 4)
-
Case TYPE_2 ' (another Const value)
-
strDetails1 = Mid$(strRecord, 16, 6) ' Overlaps above 18,15
-
strDetails2 = Mid$(strRecord, 50, 2) ' Doesn't overlap anything
-
'
-
'
-
'
-
End Select
-
So you see, I am selectively grabbing many different chunks, but generally only once per record (based on the type encoded within the record). So the sort of techniques I might normally use, like assigning all of them to variables at the start then using those variables, actually increase the processing in this case.
I don't have much experience with Byte arrays, but I gather they can be faster than strings at times. The problem is that whenever I need to check a value, I expect I'll have to loop through a section of the array, which seems inefficient. I really miss the processing available in Natural. In that (it's predominantly a mainframe language) I could just say IF ARRAY1(10:15) = ARRAY2(20:25) THEN BLAH BLAH.
kadghar 1,295
Recognized Expert Top Contributor
I see what you mean, and yes... of course making a loop through each character of a string takes longer than chopping a section only once.
Since you have n different records to chop in m different parts, you'll always have m x n different substrings, and anytime you need one of this values for the first time, you'll have to obtain it (using a mid$ i guess)... i dont see an easy way out because you're not using the any substring that much times.
(but remembering your 39 phrases of wisdom, i'd add a 40th one: dont tell anyone something is imposible while he is doing it)
Good Luck.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Brian Patterson |
last post by:
I have noticed in the book of words that hasattr works by calling getattr
and raising an exception if no such attribute exists. If I need the value
in any case, am I better off using getattr...
|
by: Kevin Frey |
last post by:
Hello,
I have a test database with table A containing 10,000 rows and a table
B containing 100,000 rows. Rows in B are "children" of rows in A -
each row in A has 10 related rows in B (ie. B has...
|
by: Gaurav |
last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250
Any comments?
Thanks
Gaurav
|
by: Nawab |
last post by:
Hey can anyone explain to me the difference between Mid and Mid$ ....i
notice sometimes when i a running the update query with Mid$ it tells
me that for example 3 records will be updated ( which in...
|
by: Andy_Khosravi |
last post by:
Hello,
I'm having a problem with the MID function within Access 97. I have
been trying to build a function to check to make sure that a field on a
form does not have any spaces or dashes. This...
| |
by: Mark Shelor |
last post by:
I've encountered a troublesome inconsistency in the C-language Perl
extension I've written for CPAN (Digest::SHA). The problem involves the
use of a static array within a performance-critical...
|
by: Don Pedro |
last post by:
According to the documentation for the DataBinder class's Eval method should
it only be used with discretion due to the fact it is latebound and uses
reflection.
"CAUTION Since this method...
|
by: Rune B |
last post by:
Hi Group
I was considering using a Generic Dictionary<> as a value container inside
my business objects, for the reason of keeping track of fields changed or
added and so on.
- But how...
|
by: Frank |
last post by:
Hi,
imagine there's a WEB application reading data from an Oracle database
to visualize in using DataGrids in the clients browser. Yes, sounds
simple, just create OracleConnection + OracleCommand...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |