Solved

Optimizing Code - please help

Posted on 2000-02-22
7
163 Views
Last Modified: 2010-05-02
Experts,
   I've been writing some code with my friend for a program (not school related!) we are working on.  Unfortunately, the code that we pumped out does not quite go as fast as what we would like.  We tried converting it to C++ code but we aren't that great with the language so it didn't exactly run much faster.  So if any some experts could help get either this VB code to run really fast or to help us out with writing a faster routine in C++ i would greatly appreciate it!  THANKS!
The code is as follows:
'------------
Dim len1 As Double
Dim ival As Double
Dim string1(10000) As String
Dim tst As Byte
Dim numoftimes As Long
 
    Open "c:\windows\desktop\test.exe" For Binary As #1
   
    'Input
    For len1 = 0 To 200000000 Step 3000  'input file
        For z = 1 To 3000  'for every 3000 bytes
            Get #1, len1 + z, tst  'input byte
            ival = (len1 + z - 1) / 3000
            string1(ival) = string1(ival) & Fill(CStr(tst), 3)'tack onto string
            If EOF(1) Then Exit For
        Next z
        numoftimes = (len1 + z - 1)
        If EOF(1) Then Exit For
    Next len1
    Close #1
'----
Private Function Fill(ByVal data As String, ByVal length As Integer) As String
 Fill = data
 If Len(data) < length Then Fill = String(length - Len(data), "0") & Fill
End Function
'----
0
Comment
Question by:TrEaSoN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2549113
What I can figure out from your code the following code should do the same thing, but about 30x faster...

  Dim a As Long
  Dim ByteSize As Long
  Dim ByteArray() As Byte
  Dim StringCount As Long
  Dim StringArray() As String
 
  ByteSize = 3000
  ReDim ByteArray(0 To ByteSize - 1)
 
  Open "c:\windows\desktop\test.exe" For Binary As #1
  ReDim StringArray(LOF(1) \ ByteSize + IIf(LOF(1) Mod ByteSize = 0, -1, 0))
  Do Until EOF(1)
    If ((LOF(1) - Seek(1) + 2) <= ByteSize) Then
      ByteSize = LOF(1) - Seek(1) + 2
      ReDim ByteArray(0 To ByteSize - 1)
    End If
    Get #1, , ByteArray
    StringCount = StringCount + 1
    StringArray(StringCount - 1) = Space$(3 * ByteSize)
    For a = 1 To ByteSize
      Mid$(StringArray(StringCount - 1), 1 + (3 * (a - 1))) = Format$(ByteArray(a - 1), "000")
    Next
  Loop
0
 
LVL 7

Accepted Solution

by:
Vbmaster earned 300 total points
ID: 2549137
Here's a improved version before I get to sleep, this one is about 50x-60x faster than your original code...

  Dim ByteSize As Long
  Dim ByteArray() As Byte
  Dim StringCount As Long
  Dim StringArray() As String
  Dim a As Long
 
  ByteSize = 3000
  ReDim ByteArray(0 To ByteSize - 1)
 
  Open "c:\windows\desktop\test.exe" For Binary As #1
  ReDim StringArray(LOF(1) \ ByteSize + IIf(LOF(1) Mod ByteSize = 0, -1, 0))
  Do Until EOF(1)
    If ((LOF(1) - Seek(1) + 2) <= ByteSize) Then
      ByteSize = LOF(1) - Seek(1) + 2
      ReDim ByteArray(0 To ByteSize - 1)
    End If
    Get #1, , ByteArray
    StringCount = StringCount + 1
    StringArray(StringCount - 1) = String$(3 * ByteSize, "0")
    For a = 1 To ByteSize
      If (ByteArray(a - 1) < 10) Then
        Mid$(StringArray(StringCount - 1), 2 + 1 + (3 * (a - 1))) = ByteArray(a - 1)
      ElseIf (ByteArray(a - 1) < 100) Then
        Mid$(StringArray(StringCount - 1), 1 + 1 + (3 * (a - 1))) = ByteArray(a - 1)
      Else
        Mid$(StringArray(StringCount - 1), 1 + (3 * (a - 1))) = ByteArray(a - 1)
      End If
    Next
  Loop
  Erase ByteArray

This new one has some new code inside the For...Next block and the String$ function is used instead of the Space$ function, the big speed improvement is because with this new version you do not need to use the (very slow) Format$ function.

But now I'm not sure if there can be any more ("major") optimizations made on this one. ;)
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2549140
What are you trying to do? I've looked over the code (and even tried it) but I can't find a single reason for it. Speeding it up is probably quite easy, but I don't know what you want it to do.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 5

Expert Comment

by:KDivad
ID: 2549144
I guess I was right, since vbmaster posted twice while I was still looking at it. ;-)
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2549161
To me it looks like the code extracts the ascii code of all the bytes inside a file, and adds it to a string value like 065066067068 for a file containing the text "ABCD"...

Perhaps this function is used in some kind of editor or perhaps Treason needs to know the ascii code for something else.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2550235
That's kinda what it looked like, but what got me was this:

ival = (len1 + z - 1) / 3000
string1(ival) =

ival SEEMED to me to be a useless procession of values (1,6,3,0,0... or something like that) when I stepped through the code. Maybe I'm missing something...(again!).

Later,
0
 

Author Comment

by:TrEaSoN
ID: 2552410
thanks for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using Access 8 96
message box in access 4 55
Modifying Conditional Format from VBA code 3 85
VBA: copy range dynamically based on config sheet v3 6 54
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question