473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance of Mid$ and possible alternatives

8,435 Recognized Expert Expert
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.
Jul 10 '07 #1
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.

Expand|Select|Wrap|Line Numbers
  1. Sub compare()
  2. Dim Str1 As String
  3. Dim Str2 As String
  4. Dim mStr1() As String
  5. Dim Sin1 As Single
  6. Dim Sin2 As Single
  7. Dim Sin3 As Single
  8. Dim i As Integer
  9. Dim j As Long
  10. Str1 = "Now i'll write here whatever it comes to my mind just to make this little test."
  11.  
  12. ReDim mStr1(1 To Len(Str1))
  13. 'Lets see how long it takes to save it into an array
  14. Sin1 = Timer
  15.     For i = 1 To Len(Str1)
  16.         mStr1(i) = Mid(Str1, i, 1)
  17.     Next
  18. Sin1 = Timer - Sin1
  19.  
  20. 'Now lets write the string upside down 1'000'000 times
  21. 'and take the time using the old mid$
  22.  
  23. Sin2 = Timer
  24. For j = 1 To 1000000
  25.     Str2 = ""
  26.     For i = Len(Str1) To 1 Step -1
  27.         Str2 = Str2 & Mid(Str1, i, 1)
  28.     Next
  29. Next
  30. Sin2 = Timer - Sin2
  31.  
  32. 'and take the time using the array
  33.  
  34. Sin3 = Timer
  35. For j = 1 To 1000000
  36.     Str2 = ""
  37.     For i = Len(Str1) To 1 Step -1
  38.         Str2 = Str2 & mStr1(i)
  39.     Next
  40. Next
  41. Sin3 = Timer - Sin3
  42.  
  43. MsgBox ("Array time: " & Sin1 & Chr(10) & "Mid$ Method: " & Sin2 & Chr(10) & "Array Method: " & Sin3)
  44.  
  45. 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.)
Jul 11 '07 #2
Killer42
8,435 Recognized Expert Expert
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)

Expand|Select|Wrap|Line Numbers
  1. strRecordType = Mid$(strRecord, 5, 8)
  2. Select Case strRecordType
  3.   Case TYPE_1 ' (a Const value)
  4.     strDetails1 = Mid$(strRecord, 18, 15)
  5.     strDetails2 = Mid$(strRecord, 56, 4)
  6.   Case TYPE_2 ' (another Const value)
  7.     strDetails1 = Mid$(strRecord, 16, 6) ' Overlaps above 18,15
  8.     strDetails2 = Mid$(strRecord, 50, 2) ' Doesn't overlap anything
  9.   '
  10.   '
  11.   '
  12. End Select
  13.  
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.
Jul 11 '07 #3
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.
Jul 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
3452
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...
1
7397
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...
133
8425
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
6
27596
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...
4
6013
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...
115
7474
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...
0
2488
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...
18
14951
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...
9
2195
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...
0
7153
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...
0
7373
Oralloy
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,...
1
7094
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...
0
5677
agi2029
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,...
1
5079
isladogs
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.