Speed2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Speed2

  1. #1
    Ing. Roberto Nisthal Guest

    Re: Speed2

    > It's extremely slow..... about 30 min to load a 350k file.
    String concatenation is the culprit...
    Several suggestions

    1) Get the size of the file you are handling
    2) Allocate enough space in a buffer, for example using the space function
    3) Read the file in bigger chunks
    4) DON'T USE STRING CONCATENATION, if you need a string use something like
    mid$(Result,i,1) = Character



  2. #2
    Ing. Roberto Nisthal Guest

    Re: Speed2

    > It's extremely slow..... about 30 min to load a 350k file.
    String concatenation is the culprit...
    Several suggestions

    1) Get the size of the file you are handling
    2) Allocate enough space in a buffer, for example using the space function
    3) Read the file in bigger chunks
    4) DON'T USE STRING CONCATENATION, if you need a string use something like
    mid$(Result,i,1) = Character



  3. #3
    Shawn Wilson Guest

    Speed2


    Here is a challenging one....

    Below is a function which will open a file and return a string representation
    of the hexadecimal
    contents of that file.

    It's extremely slow..... about 30 min to load a 350k file.

    Here comes the fun....how can I modify this function to perform in < 10 seconds
    (+/- a few)
    without having to get a faster computer or having to write a function in
    C.

    Do you think using the windows API will make things faster? If so, can someone
    give me a VB function
    using the windows API to do this?

    I should have paid attention in my data structures and algorithm class. )

    Thanks to anyone/everyone who has a way to do this.



    Public Function OpenWithString() As String
    Dim Character As String


    Dim Variable As Byte
    Dim Result As String
    Dim i As Long

    i = 0

    Open "C:\temp\jde_docs_79.zip" For Binary Access Read As #1 ' Open file
    Result = "0x"
    Do While Not EOF(1)

    Get #1, , Variable
    Character = Hex(Variable)
    If Len(Character) = 1 Then
    Character = "0" & Character
    End If
    Result = Result & Character
    Loop

    ' Result now holds the complete string (which is 2 times the original file
    length plus 4 bytes)

    Close #1

    OpenWithString = Result

    End Function

  4. #4
    Bruno Paris Guest

    Re: Speed2

    Hi, Shawn
    Here is the code:

    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long

    Private Sub Form_Click()
    Dim s As String, tim As Long
    tim = GetTickCount ' start timer

    s = OpenWithString

    Caption = (GetTickCount - tim) / 1000 ' report time
    End Sub

    Public Function OpenWithString() As String
    Dim b() As Byte
    Dim flen As Long, i As Long
    Static hextable(255) As String
    Static initDone As Boolean

    If Not initDone Then ' do this only once
    initDone = True
    For i = 0 To 15
    hextable(i) = "0" & Hex(i)
    Next
    For i = 16 To 255
    hextable(i) = Hex(i)
    Next
    End If

    ' Read the file into byte array
    Open "C:\temp\jde_docs_79.zip" For Binary _
    Access Read Lock Read Write As #1
    flen = LOF(1)
    ReDim b(0 To flen - 1) As Byte
    Get #1, , b()
    Close #1

    ' prepare big string
    OpenWithString = Space(2 + 2 * flen)

    ' fill the string (non-API method)
    Mid$(OpenWithString, 1, 2) = "0x"
    For i = 0 To flen - 1
    Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    Next
    End Function


    When compiling, use advanced optimizations (remove bounds
    check, remove integer overflow checks)

    Regards,
    Bruno


    Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    >
    >Here is a challenging one....

    ....



  5. #5
    Bruno Paris Guest

    Re: Speed2

    Hi, Shawn
    Here is the code:

    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long

    Private Sub Form_Click()
    Dim s As String, tim As Long
    tim = GetTickCount ' start timer

    s = OpenWithString

    Caption = (GetTickCount - tim) / 1000 ' report time
    End Sub

    Public Function OpenWithString() As String
    Dim b() As Byte
    Dim flen As Long, i As Long
    Static hextable(255) As String
    Static initDone As Boolean

    If Not initDone Then ' do this only once
    initDone = True
    For i = 0 To 15
    hextable(i) = "0" & Hex(i)
    Next
    For i = 16 To 255
    hextable(i) = Hex(i)
    Next
    End If

    ' Read the file into byte array
    Open "C:\temp\jde_docs_79.zip" For Binary _
    Access Read Lock Read Write As #1
    flen = LOF(1)
    ReDim b(0 To flen - 1) As Byte
    Get #1, , b()
    Close #1

    ' prepare big string
    OpenWithString = Space(2 + 2 * flen)

    ' fill the string (non-API method)
    Mid$(OpenWithString, 1, 2) = "0x"
    For i = 0 To flen - 1
    Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    Next
    End Function


    When compiling, use advanced optimizations (remove bounds
    check, remove integer overflow checks)

    Regards,
    Bruno


    Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    >
    >Here is a challenging one....

    ....



  6. #6
    Shawn Wilson Guest

    Re: Speed2


    Bruno, Roberto:

    Thankyou very much for your answers. This code is code is sharp.
    It would have taken me a long time to figure this out.

    This code is great.....amazingly much faster than my original implementation.

    Thank a lot again guys...

    Are there any good data structures and algorithms books for VB that you may
    know of?

    Shawn


    "Bruno Paris" <nospam_ameba@zg.tel.hr> wrote:
    >Hi, Shawn
    >Here is the code:
    >
    >Option Explicit
    >Private Declare Function GetTickCount Lib "kernel32" () As Long
    >
    >Private Sub Form_Click()
    > Dim s As String, tim As Long
    > tim = GetTickCount ' start timer
    >
    > s = OpenWithString
    >
    > Caption = (GetTickCount - tim) / 1000 ' report time
    >End Sub
    >
    >Public Function OpenWithString() As String
    > Dim b() As Byte
    > Dim flen As Long, i As Long
    > Static hextable(255) As String
    > Static initDone As Boolean
    >
    > If Not initDone Then ' do this only once
    > initDone = True
    > For i = 0 To 15
    > hextable(i) = "0" & Hex(i)
    > Next
    > For i = 16 To 255
    > hextable(i) = Hex(i)
    > Next
    > End If
    >
    > ' Read the file into byte array
    > Open "C:\temp\jde_docs_79.zip" For Binary _
    > Access Read Lock Read Write As #1
    > flen = LOF(1)
    > ReDim b(0 To flen - 1) As Byte
    > Get #1, , b()
    > Close #1
    >
    > ' prepare big string
    > OpenWithString = Space(2 + 2 * flen)
    >
    > ' fill the string (non-API method)
    > Mid$(OpenWithString, 1, 2) = "0x"
    > For i = 0 To flen - 1
    > Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    > Next
    >End Function
    >
    >
    >When compiling, use advanced optimizations (remove bounds
    > check, remove integer overflow checks)
    >
    >Regards,
    >Bruno
    >
    >
    >Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    >>
    >>Here is a challenging one....

    >....
    >
    >



  7. #7
    Shawn Wilson Guest

    Re: Speed2


    Bruno, Roberto:

    Thankyou very much for your answers. This code is code is sharp.
    It would have taken me a long time to figure this out.

    This code is great.....amazingly much faster than my original implementation.

    Thank a lot again guys...

    Are there any good data structures and algorithms books for VB that you may
    know of?

    Shawn


    "Bruno Paris" <nospam_ameba@zg.tel.hr> wrote:
    >Hi, Shawn
    >Here is the code:
    >
    >Option Explicit
    >Private Declare Function GetTickCount Lib "kernel32" () As Long
    >
    >Private Sub Form_Click()
    > Dim s As String, tim As Long
    > tim = GetTickCount ' start timer
    >
    > s = OpenWithString
    >
    > Caption = (GetTickCount - tim) / 1000 ' report time
    >End Sub
    >
    >Public Function OpenWithString() As String
    > Dim b() As Byte
    > Dim flen As Long, i As Long
    > Static hextable(255) As String
    > Static initDone As Boolean
    >
    > If Not initDone Then ' do this only once
    > initDone = True
    > For i = 0 To 15
    > hextable(i) = "0" & Hex(i)
    > Next
    > For i = 16 To 255
    > hextable(i) = Hex(i)
    > Next
    > End If
    >
    > ' Read the file into byte array
    > Open "C:\temp\jde_docs_79.zip" For Binary _
    > Access Read Lock Read Write As #1
    > flen = LOF(1)
    > ReDim b(0 To flen - 1) As Byte
    > Get #1, , b()
    > Close #1
    >
    > ' prepare big string
    > OpenWithString = Space(2 + 2 * flen)
    >
    > ' fill the string (non-API method)
    > Mid$(OpenWithString, 1, 2) = "0x"
    > For i = 0 To flen - 1
    > Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    > Next
    >End Function
    >
    >
    >When compiling, use advanced optimizations (remove bounds
    > check, remove integer overflow checks)
    >
    >Regards,
    >Bruno
    >
    >
    >Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    >>
    >>Here is a challenging one....

    >....
    >
    >



  8. #8
    Bruno Paris Guest

    Re: Speed2

    Hi, Shawn

    >Bruno, Roberto:
    >
    >Thankyou very much for your answers.

    You are welcome.

    See also this article: Play VB's Strings
    http://www.vb2themax.com/HtmlDoc.asp...Articles&ID=30

    Regards,
    Bruno



  9. #9
    Bruno Paris Guest

    Re: Speed2

    Hi, Shawn

    >Bruno, Roberto:
    >
    >Thankyou very much for your answers.

    You are welcome.

    See also this article: Play VB's Strings
    http://www.vb2themax.com/HtmlDoc.asp...Articles&ID=30

    Regards,
    Bruno



  10. #10
    Eric D. Burdo Guest

    Re: Speed2

    Rod Stephens has a good algorithms book...

    http://www.amazon.com/exec/obidos/AS...2-7980153-2040
    942

    --

    Eric D. Burdo, Red-Leif International
    VB Programmer and Consultant
    <http://www.redleif.com/vb>

    *** Please reply to the newsgroup so all can benefit. ***


    "Shawn Wilson" <wilso_s@mailcity.com> wrote in message
    news:3989eb97$1@news.devx.com...
    >
    > Bruno, Roberto:
    >
    > Thankyou very much for your answers. This code is code is sharp.
    > It would have taken me a long time to figure this out.
    >
    > This code is great.....amazingly much faster than my original

    implementation.
    >
    > Thank a lot again guys...
    >
    > Are there any good data structures and algorithms books for VB that you

    may
    > know of?
    >
    > Shawn
    >
    >
    > "Bruno Paris" <nospam_ameba@zg.tel.hr> wrote:
    > >Hi, Shawn
    > >Here is the code:
    > >
    > >Option Explicit
    > >Private Declare Function GetTickCount Lib "kernel32" () As Long
    > >
    > >Private Sub Form_Click()
    > > Dim s As String, tim As Long
    > > tim = GetTickCount ' start timer
    > >
    > > s = OpenWithString
    > >
    > > Caption = (GetTickCount - tim) / 1000 ' report time
    > >End Sub
    > >
    > >Public Function OpenWithString() As String
    > > Dim b() As Byte
    > > Dim flen As Long, i As Long
    > > Static hextable(255) As String
    > > Static initDone As Boolean
    > >
    > > If Not initDone Then ' do this only once
    > > initDone = True
    > > For i = 0 To 15
    > > hextable(i) = "0" & Hex(i)
    > > Next
    > > For i = 16 To 255
    > > hextable(i) = Hex(i)
    > > Next
    > > End If
    > >
    > > ' Read the file into byte array
    > > Open "C:\temp\jde_docs_79.zip" For Binary _
    > > Access Read Lock Read Write As #1
    > > flen = LOF(1)
    > > ReDim b(0 To flen - 1) As Byte
    > > Get #1, , b()
    > > Close #1
    > >
    > > ' prepare big string
    > > OpenWithString = Space(2 + 2 * flen)
    > >
    > > ' fill the string (non-API method)
    > > Mid$(OpenWithString, 1, 2) = "0x"
    > > For i = 0 To flen - 1
    > > Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    > > Next
    > >End Function
    > >
    > >
    > >When compiling, use advanced optimizations (remove bounds
    > > check, remove integer overflow checks)
    > >
    > >Regards,
    > >Bruno
    > >
    > >
    > >Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    > >>
    > >>Here is a challenging one....

    > >....
    > >
    > >

    >




  11. #11
    Eric D. Burdo Guest

    Re: Speed2

    Rod Stephens has a good algorithms book...

    http://www.amazon.com/exec/obidos/AS...2-7980153-2040
    942

    --

    Eric D. Burdo, Red-Leif International
    VB Programmer and Consultant
    <http://www.redleif.com/vb>

    *** Please reply to the newsgroup so all can benefit. ***


    "Shawn Wilson" <wilso_s@mailcity.com> wrote in message
    news:3989eb97$1@news.devx.com...
    >
    > Bruno, Roberto:
    >
    > Thankyou very much for your answers. This code is code is sharp.
    > It would have taken me a long time to figure this out.
    >
    > This code is great.....amazingly much faster than my original

    implementation.
    >
    > Thank a lot again guys...
    >
    > Are there any good data structures and algorithms books for VB that you

    may
    > know of?
    >
    > Shawn
    >
    >
    > "Bruno Paris" <nospam_ameba@zg.tel.hr> wrote:
    > >Hi, Shawn
    > >Here is the code:
    > >
    > >Option Explicit
    > >Private Declare Function GetTickCount Lib "kernel32" () As Long
    > >
    > >Private Sub Form_Click()
    > > Dim s As String, tim As Long
    > > tim = GetTickCount ' start timer
    > >
    > > s = OpenWithString
    > >
    > > Caption = (GetTickCount - tim) / 1000 ' report time
    > >End Sub
    > >
    > >Public Function OpenWithString() As String
    > > Dim b() As Byte
    > > Dim flen As Long, i As Long
    > > Static hextable(255) As String
    > > Static initDone As Boolean
    > >
    > > If Not initDone Then ' do this only once
    > > initDone = True
    > > For i = 0 To 15
    > > hextable(i) = "0" & Hex(i)
    > > Next
    > > For i = 16 To 255
    > > hextable(i) = Hex(i)
    > > Next
    > > End If
    > >
    > > ' Read the file into byte array
    > > Open "C:\temp\jde_docs_79.zip" For Binary _
    > > Access Read Lock Read Write As #1
    > > flen = LOF(1)
    > > ReDim b(0 To flen - 1) As Byte
    > > Get #1, , b()
    > > Close #1
    > >
    > > ' prepare big string
    > > OpenWithString = Space(2 + 2 * flen)
    > >
    > > ' fill the string (non-API method)
    > > Mid$(OpenWithString, 1, 2) = "0x"
    > > For i = 0 To flen - 1
    > > Mid$(OpenWithString, 3 + 2 * i, 2) = hextable(b(i))
    > > Next
    > >End Function
    > >
    > >
    > >When compiling, use advanced optimizations (remove bounds
    > > check, remove integer overflow checks)
    > >
    > >Regards,
    > >Bruno
    > >
    > >
    > >Shawn Wilson wrote in message <39874c4a$1@news.devx.com>...
    > >>
    > >>Here is a challenging one....

    > >....
    > >
    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center