DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Speed

  1. #1
    Shawn Wilson Guest

    Speed


    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

  2. #2
    Jim Pragit Guest

    Re: Speed


    Shawn,

    String functions are very slow in VB. In particular, when you're doing lots
    of concatenation, VB is constantly reallocating and deallocating memory.
    One way to solve your problem is to pre-allocate the memory in advance.
    Most likely, this should solve your problem. In addition, you may wish
    to change your function to a subroutine and pass the resulting string by
    reference.

    Here are two modified versions of your routines. Just change the PATHNAME
    constant to whatever file you want to process.

    Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"

    'as a function
    Public Function OpenWithString1() As String
    Dim strCharacter As String
    Dim bytVariable As Byte
    Dim strResult As String
    Dim intFileNbr As Integer
    Dim lngPos As Long

    strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    lngPos = 1

    intFileNbr = FreeFile
    Open PATHNAME For Binary Access Read As #intFileNbr

    Do While Not EOF(intFileNbr)

    Get #intFileNbr, , bytVariable
    strCharacter = Hex(bytVariable)
    If Len(strCharacter) = 1 Then
    strCharacter = "0" & strCharacter
    End If
    Mid$(strResult, lngPos, 2) = strCharacter
    lngPos = lngPos + 2
    Loop
    Close #intFileNbr

    OpenWithString1 = strResult

    End Function

    'as a subroutine with the resulting string being passed by reference
    Public Sub OpenWithString2(ByRef Result As String)
    Dim strCharacter As String
    Dim bytVariable As Byte
    Dim intFileNbr As Integer
    Dim lngPos As Long

    Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    lngPos = 1

    intFileNbr = FreeFile
    Open PATHNAME For Binary Access Read As #intFileNbr

    Do While Not EOF(intFileNbr)

    Get #intFileNbr, , bytVariable
    strCharacter = Hex(bytVariable)
    If Len(strCharacter) = 1 Then
    strCharacter = "0" & strCharacter
    End If
    Mid$(Result, lngPos, 2) = strCharacter
    lngPos = lngPos + 2
    Loop
    Close #intFileNbr

    End Sub

    - Jim

    "Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >
    >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



  3. #3
    A A Guest

    Re: Speed


    If you have to read the whole file anyway, might as well read it all in one
    go. All those disk i/o calls can be expensive.

    Buffer=String(fileLen(FileName)," ")
    Open FileName for Binary Access Read as #File
    Get #File,,Buffer



    "Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >
    >Shawn,
    >
    >String functions are very slow in VB. In particular, when you're doing

    lots
    >of concatenation, VB is constantly reallocating and deallocating memory.
    > One way to solve your problem is to pre-allocate the memory in advance.
    > Most likely, this should solve your problem. In addition, you may wish
    >to change your function to a subroutine and pass the resulting string by
    >reference.
    >
    >Here are two modified versions of your routines. Just change the PATHNAME
    >constant to whatever file you want to process.
    >
    >Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >
    >'as a function
    >Public Function OpenWithString1() As String
    > Dim strCharacter As String
    > Dim bytVariable As Byte
    > Dim strResult As String
    > Dim intFileNbr As Integer
    > Dim lngPos As Long
    >
    > strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    > lngPos = 1
    >
    > intFileNbr = FreeFile
    > Open PATHNAME For Binary Access Read As #intFileNbr
    >
    > Do While Not EOF(intFileNbr)
    >
    > Get #intFileNbr, , bytVariable
    > strCharacter = Hex(bytVariable)
    > If Len(strCharacter) = 1 Then
    > strCharacter = "0" & strCharacter
    > End If
    > Mid$(strResult, lngPos, 2) = strCharacter
    > lngPos = lngPos + 2
    > Loop
    > Close #intFileNbr
    >
    > OpenWithString1 = strResult
    >
    >End Function
    >
    >'as a subroutine with the resulting string being passed by reference
    >Public Sub OpenWithString2(ByRef Result As String)
    > Dim strCharacter As String
    > Dim bytVariable As Byte
    > Dim intFileNbr As Integer
    > Dim lngPos As Long
    >
    > Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    > lngPos = 1
    >
    > intFileNbr = FreeFile
    > Open PATHNAME For Binary Access Read As #intFileNbr
    >
    > Do While Not EOF(intFileNbr)
    >
    > Get #intFileNbr, , bytVariable
    > strCharacter = Hex(bytVariable)
    > If Len(strCharacter) = 1 Then
    > strCharacter = "0" & strCharacter
    > End If
    > Mid$(Result, lngPos, 2) = strCharacter
    > lngPos = lngPos + 2
    > Loop
    > Close #intFileNbr
    >
    >End Sub
    >
    >- Jim
    >
    >"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>
    >>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
    Jim Pragit Guest

    Re: Speed


    Well, it's not a bad suggestion in general, but I don't think
    it's a good idea in this particular instance. The OP wants
    to edit the file's contents:

    strCharacter = Hex(bytVariable)
    If Len(strCharacter) = 1 Then
    strCharacter = "0" & strCharacter
    End If

    If you read the entire file all at once, you're going to have
    perform the string manipulation with the entire string in
    memory. For example, if the first byte was "1", it must be
    modified to be "01". Therefore, you have to shift every
    subsequent character over one position. This constant
    shifting will probably cause more hard than good. Maybe
    you know a way around this? If you want, go ahead and
    write your version of OpenWithString. I'll run a benchmark
    and find out if your suggestion helps.

    - Jim


    "A" A wrote:
    >
    >If you have to read the whole file anyway, might as well read it all in

    one
    >go. All those disk i/o calls can be expensive.
    >
    > Buffer=String(fileLen(FileName)," ")
    > Open FileName for Binary Access Read as #File
    > Get #File,,Buffer
    >
    >
    >
    >"Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >>
    >>Shawn,
    >>
    >>String functions are very slow in VB. In particular, when you're doing

    >lots
    >>of concatenation, VB is constantly reallocating and deallocating memory.
    >> One way to solve your problem is to pre-allocate the memory in advance.
    >> Most likely, this should solve your problem. In addition, you may wish
    >>to change your function to a subroutine and pass the resulting string by
    >>reference.
    >>
    >>Here are two modified versions of your routines. Just change the PATHNAME
    >>constant to whatever file you want to process.
    >>
    >>Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >>
    >>'as a function
    >>Public Function OpenWithString1() As String
    >> Dim strCharacter As String
    >> Dim bytVariable As Byte
    >> Dim strResult As String
    >> Dim intFileNbr As Integer
    >> Dim lngPos As Long
    >>
    >> strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    >> lngPos = 1
    >>
    >> intFileNbr = FreeFile
    >> Open PATHNAME For Binary Access Read As #intFileNbr
    >>
    >> Do While Not EOF(intFileNbr)
    >>
    >> Get #intFileNbr, , bytVariable
    >> strCharacter = Hex(bytVariable)
    >> If Len(strCharacter) = 1 Then
    >> strCharacter = "0" & strCharacter
    >> End If
    >> Mid$(strResult, lngPos, 2) = strCharacter
    >> lngPos = lngPos + 2
    >> Loop
    >> Close #intFileNbr
    >>
    >> OpenWithString1 = strResult
    >>
    >>End Function
    >>
    >>'as a subroutine with the resulting string being passed by reference
    >>Public Sub OpenWithString2(ByRef Result As String)
    >> Dim strCharacter As String
    >> Dim bytVariable As Byte
    >> Dim intFileNbr As Integer
    >> Dim lngPos As Long
    >>
    >> Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    >> lngPos = 1
    >>
    >> intFileNbr = FreeFile
    >> Open PATHNAME For Binary Access Read As #intFileNbr
    >>
    >> Do While Not EOF(intFileNbr)
    >>
    >> Get #intFileNbr, , bytVariable
    >> strCharacter = Hex(bytVariable)
    >> If Len(strCharacter) = 1 Then
    >> strCharacter = "0" & strCharacter
    >> End If
    >> Mid$(Result, lngPos, 2) = strCharacter
    >> lngPos = lngPos + 2
    >> Loop
    >> Close #intFileNbr
    >>
    >>End Sub
    >>
    >>- Jim
    >>
    >>"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>>
    >>>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

    >>

    >



  5. #5
    challis Guest

    Re: Speed


    Rading the entire file in one operation is definatly much, much faster. (I
    would guess anything from 150 - 200 times)

    Then iterate through the characters (Mid$) and add them to an array (which
    you can dimension accurately right after the read ie. no redims) of the desired
    type.




    "Jim Pragit" <emunews@msn.com> wrote:
    >
    >Well, it's not a bad suggestion in general, but I don't think
    >it's a good idea in this particular instance. The OP wants
    >to edit the file's contents:
    >
    > strCharacter = Hex(bytVariable)
    > If Len(strCharacter) = 1 Then
    > strCharacter = "0" & strCharacter
    > End If
    >
    >If you read the entire file all at once, you're going to have
    >perform the string manipulation with the entire string in
    >memory. For example, if the first byte was "1", it must be
    >modified to be "01". Therefore, you have to shift every
    >subsequent character over one position. This constant
    >shifting will probably cause more hard than good. Maybe
    >you know a way around this? If you want, go ahead and
    >write your version of OpenWithString. I'll run a benchmark
    >and find out if your suggestion helps.
    >
    >- Jim
    >
    >
    >"A" A wrote:
    >>
    >>If you have to read the whole file anyway, might as well read it all in

    >one
    >>go. All those disk i/o calls can be expensive.
    >>
    >> Buffer=String(fileLen(FileName)," ")
    >> Open FileName for Binary Access Read as #File
    >> Get #File,,Buffer
    >>
    >>
    >>
    >>"Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >>>
    >>>Shawn,
    >>>
    >>>String functions are very slow in VB. In particular, when you're doing

    >>lots
    >>>of concatenation, VB is constantly reallocating and deallocating memory.
    >>> One way to solve your problem is to pre-allocate the memory in advance.
    >>> Most likely, this should solve your problem. In addition, you may wish
    >>>to change your function to a subroutine and pass the resulting string

    by
    >>>reference.
    >>>
    >>>Here are two modified versions of your routines. Just change the PATHNAME
    >>>constant to whatever file you want to process.
    >>>
    >>>Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >>>
    >>>'as a function
    >>>Public Function OpenWithString1() As String
    >>> Dim strCharacter As String
    >>> Dim bytVariable As Byte
    >>> Dim strResult As String
    >>> Dim intFileNbr As Integer
    >>> Dim lngPos As Long
    >>>
    >>> strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>> lngPos = 1
    >>>
    >>> intFileNbr = FreeFile
    >>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>
    >>> Do While Not EOF(intFileNbr)
    >>>
    >>> Get #intFileNbr, , bytVariable
    >>> strCharacter = Hex(bytVariable)
    >>> If Len(strCharacter) = 1 Then
    >>> strCharacter = "0" & strCharacter
    >>> End If
    >>> Mid$(strResult, lngPos, 2) = strCharacter
    >>> lngPos = lngPos + 2
    >>> Loop
    >>> Close #intFileNbr
    >>>
    >>> OpenWithString1 = strResult
    >>>
    >>>End Function
    >>>
    >>>'as a subroutine with the resulting string being passed by reference
    >>>Public Sub OpenWithString2(ByRef Result As String)
    >>> Dim strCharacter As String
    >>> Dim bytVariable As Byte
    >>> Dim intFileNbr As Integer
    >>> Dim lngPos As Long
    >>>
    >>> Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>> lngPos = 1
    >>>
    >>> intFileNbr = FreeFile
    >>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>
    >>> Do While Not EOF(intFileNbr)
    >>>
    >>> Get #intFileNbr, , bytVariable
    >>> strCharacter = Hex(bytVariable)
    >>> If Len(strCharacter) = 1 Then
    >>> strCharacter = "0" & strCharacter
    >>> End If
    >>> Mid$(Result, lngPos, 2) = strCharacter
    >>> lngPos = lngPos + 2
    >>> Loop
    >>> Close #intFileNbr
    >>>
    >>>End Sub
    >>>
    >>>- Jim
    >>>
    >>>"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>>>
    >>>>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
    >>>

    >>

    >



  6. #6
    Jim Pragit Guest

    Re: Speed


    Post it and we'll benchmark.

    - Jim

    "challis" <bmlx@yahoo.com> wrote:
    >
    >Rading the entire file in one operation is definatly much, much faster.

    (I
    >would guess anything from 150 - 200 times)
    >
    >Then iterate through the characters (Mid$) and add them to an array (which
    >you can dimension accurately right after the read ie. no redims) of the

    desired
    >type.
    >
    >
    >
    >
    >"Jim Pragit" <emunews@msn.com> wrote:
    >>
    >>Well, it's not a bad suggestion in general, but I don't think
    >>it's a good idea in this particular instance. The OP wants
    >>to edit the file's contents:
    >>
    >> strCharacter = Hex(bytVariable)
    >> If Len(strCharacter) = 1 Then
    >> strCharacter = "0" & strCharacter
    >> End If
    >>
    >>If you read the entire file all at once, you're going to have
    >>perform the string manipulation with the entire string in
    >>memory. For example, if the first byte was "1", it must be
    >>modified to be "01". Therefore, you have to shift every
    >>subsequent character over one position. This constant
    >>shifting will probably cause more hard than good. Maybe
    >>you know a way around this? If you want, go ahead and
    >>write your version of OpenWithString. I'll run a benchmark
    >>and find out if your suggestion helps.
    >>
    >>- Jim
    >>
    >>
    >>"A" A wrote:
    >>>
    >>>If you have to read the whole file anyway, might as well read it all in

    >>one
    >>>go. All those disk i/o calls can be expensive.
    >>>
    >>> Buffer=String(fileLen(FileName)," ")
    >>> Open FileName for Binary Access Read as #File
    >>> Get #File,,Buffer
    >>>
    >>>
    >>>
    >>>"Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >>>>
    >>>>Shawn,
    >>>>
    >>>>String functions are very slow in VB. In particular, when you're doing
    >>>lots
    >>>>of concatenation, VB is constantly reallocating and deallocating memory.
    >>>> One way to solve your problem is to pre-allocate the memory in advance.
    >>>> Most likely, this should solve your problem. In addition, you may wish
    >>>>to change your function to a subroutine and pass the resulting string

    >by
    >>>>reference.
    >>>>
    >>>>Here are two modified versions of your routines. Just change the PATHNAME
    >>>>constant to whatever file you want to process.
    >>>>
    >>>>Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >>>>
    >>>>'as a function
    >>>>Public Function OpenWithString1() As String
    >>>> Dim strCharacter As String
    >>>> Dim bytVariable As Byte
    >>>> Dim strResult As String
    >>>> Dim intFileNbr As Integer
    >>>> Dim lngPos As Long
    >>>>
    >>>> strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>> lngPos = 1
    >>>>
    >>>> intFileNbr = FreeFile
    >>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>
    >>>> Do While Not EOF(intFileNbr)
    >>>>
    >>>> Get #intFileNbr, , bytVariable
    >>>> strCharacter = Hex(bytVariable)
    >>>> If Len(strCharacter) = 1 Then
    >>>> strCharacter = "0" & strCharacter
    >>>> End If
    >>>> Mid$(strResult, lngPos, 2) = strCharacter
    >>>> lngPos = lngPos + 2
    >>>> Loop
    >>>> Close #intFileNbr
    >>>>
    >>>> OpenWithString1 = strResult
    >>>>
    >>>>End Function
    >>>>
    >>>>'as a subroutine with the resulting string being passed by reference
    >>>>Public Sub OpenWithString2(ByRef Result As String)
    >>>> Dim strCharacter As String
    >>>> Dim bytVariable As Byte
    >>>> Dim intFileNbr As Integer
    >>>> Dim lngPos As Long
    >>>>
    >>>> Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>> lngPos = 1
    >>>>
    >>>> intFileNbr = FreeFile
    >>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>
    >>>> Do While Not EOF(intFileNbr)
    >>>>
    >>>> Get #intFileNbr, , bytVariable
    >>>> strCharacter = Hex(bytVariable)
    >>>> If Len(strCharacter) = 1 Then
    >>>> strCharacter = "0" & strCharacter
    >>>> End If
    >>>> Mid$(Result, lngPos, 2) = strCharacter
    >>>> lngPos = lngPos + 2
    >>>> Loop
    >>>> Close #intFileNbr
    >>>>
    >>>>End Sub
    >>>>
    >>>>- Jim
    >>>>
    >>>>"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>>>>
    >>>>>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
    >>>>
    >>>

    >>

    >



  7. #7
    Shawn Wilson Guest

    Re: Speed


    Who won the benchmark?


    "Jim Pragit" <emunews@msn.com> wrote:
    >
    >Post it and we'll benchmark.
    >
    >- Jim
    >
    >"challis" <bmlx@yahoo.com> wrote:
    >>
    >>Rading the entire file in one operation is definatly much, much faster.

    >(I
    >>would guess anything from 150 - 200 times)
    >>
    >>Then iterate through the characters (Mid$) and add them to an array (which
    >>you can dimension accurately right after the read ie. no redims) of the

    >desired
    >>type.
    >>
    >>
    >>
    >>
    >>"Jim Pragit" <emunews@msn.com> wrote:
    >>>
    >>>Well, it's not a bad suggestion in general, but I don't think
    >>>it's a good idea in this particular instance. The OP wants
    >>>to edit the file's contents:
    >>>
    >>> strCharacter = Hex(bytVariable)
    >>> If Len(strCharacter) = 1 Then
    >>> strCharacter = "0" & strCharacter
    >>> End If
    >>>
    >>>If you read the entire file all at once, you're going to have
    >>>perform the string manipulation with the entire string in
    >>>memory. For example, if the first byte was "1", it must be
    >>>modified to be "01". Therefore, you have to shift every
    >>>subsequent character over one position. This constant
    >>>shifting will probably cause more hard than good. Maybe
    >>>you know a way around this? If you want, go ahead and
    >>>write your version of OpenWithString. I'll run a benchmark
    >>>and find out if your suggestion helps.
    >>>
    >>>- Jim
    >>>
    >>>
    >>>"A" A wrote:
    >>>>
    >>>>If you have to read the whole file anyway, might as well read it all

    in
    >>>one
    >>>>go. All those disk i/o calls can be expensive.
    >>>>
    >>>> Buffer=String(fileLen(FileName)," ")
    >>>> Open FileName for Binary Access Read as #File
    >>>> Get #File,,Buffer
    >>>>
    >>>>
    >>>>
    >>>>"Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >>>>>
    >>>>>Shawn,
    >>>>>
    >>>>>String functions are very slow in VB. In particular, when you're doing
    >>>>lots
    >>>>>of concatenation, VB is constantly reallocating and deallocating memory.
    >>>>> One way to solve your problem is to pre-allocate the memory in advance.
    >>>>> Most likely, this should solve your problem. In addition, you may

    wish
    >>>>>to change your function to a subroutine and pass the resulting string

    >>by
    >>>>>reference.
    >>>>>
    >>>>>Here are two modified versions of your routines. Just change the PATHNAME
    >>>>>constant to whatever file you want to process.
    >>>>>
    >>>>>Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >>>>>
    >>>>>'as a function
    >>>>>Public Function OpenWithString1() As String
    >>>>> Dim strCharacter As String
    >>>>> Dim bytVariable As Byte
    >>>>> Dim strResult As String
    >>>>> Dim intFileNbr As Integer
    >>>>> Dim lngPos As Long
    >>>>>
    >>>>> strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>>> lngPos = 1
    >>>>>
    >>>>> intFileNbr = FreeFile
    >>>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>>
    >>>>> Do While Not EOF(intFileNbr)
    >>>>>
    >>>>> Get #intFileNbr, , bytVariable
    >>>>> strCharacter = Hex(bytVariable)
    >>>>> If Len(strCharacter) = 1 Then
    >>>>> strCharacter = "0" & strCharacter
    >>>>> End If
    >>>>> Mid$(strResult, lngPos, 2) = strCharacter
    >>>>> lngPos = lngPos + 2
    >>>>> Loop
    >>>>> Close #intFileNbr
    >>>>>
    >>>>> OpenWithString1 = strResult
    >>>>>
    >>>>>End Function
    >>>>>
    >>>>>'as a subroutine with the resulting string being passed by reference
    >>>>>Public Sub OpenWithString2(ByRef Result As String)
    >>>>> Dim strCharacter As String
    >>>>> Dim bytVariable As Byte
    >>>>> Dim intFileNbr As Integer
    >>>>> Dim lngPos As Long
    >>>>>
    >>>>> Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>>> lngPos = 1
    >>>>>
    >>>>> intFileNbr = FreeFile
    >>>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>>
    >>>>> Do While Not EOF(intFileNbr)
    >>>>>
    >>>>> Get #intFileNbr, , bytVariable
    >>>>> strCharacter = Hex(bytVariable)
    >>>>> If Len(strCharacter) = 1 Then
    >>>>> strCharacter = "0" & strCharacter
    >>>>> End If
    >>>>> Mid$(Result, lngPos, 2) = strCharacter
    >>>>> lngPos = lngPos + 2
    >>>>> Loop
    >>>>> Close #intFileNbr
    >>>>>
    >>>>>End Sub
    >>>>>
    >>>>>- Jim
    >>>>>
    >>>>>"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>>>>>
    >>>>>>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
    >>>>>
    >>>>
    >>>

    >>

    >



  8. #8
    Jim Pragit Guest

    Re: Speed


    Nobody else posted their code, other than the original poster and myself.
    In any case, the main bottleneck of the routine was the string handling
    (which I fixed) not the file I/O.

    - Jim

    "Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >
    >Who won the benchmark?
    >
    >
    >"Jim Pragit" <emunews@msn.com> wrote:
    >>
    >>Post it and we'll benchmark.
    >>
    >>- Jim
    >>
    >>"challis" <bmlx@yahoo.com> wrote:
    >>>
    >>>Rading the entire file in one operation is definatly much, much faster.

    >>(I
    >>>would guess anything from 150 - 200 times)
    >>>
    >>>Then iterate through the characters (Mid$) and add them to an array (which
    >>>you can dimension accurately right after the read ie. no redims) of the

    >>desired
    >>>type.
    >>>
    >>>
    >>>
    >>>
    >>>"Jim Pragit" <emunews@msn.com> wrote:
    >>>>
    >>>>Well, it's not a bad suggestion in general, but I don't think
    >>>>it's a good idea in this particular instance. The OP wants
    >>>>to edit the file's contents:
    >>>>
    >>>> strCharacter = Hex(bytVariable)
    >>>> If Len(strCharacter) = 1 Then
    >>>> strCharacter = "0" & strCharacter
    >>>> End If
    >>>>
    >>>>If you read the entire file all at once, you're going to have
    >>>>perform the string manipulation with the entire string in
    >>>>memory. For example, if the first byte was "1", it must be
    >>>>modified to be "01". Therefore, you have to shift every
    >>>>subsequent character over one position. This constant
    >>>>shifting will probably cause more hard than good. Maybe
    >>>>you know a way around this? If you want, go ahead and
    >>>>write your version of OpenWithString. I'll run a benchmark
    >>>>and find out if your suggestion helps.
    >>>>
    >>>>- Jim
    >>>>
    >>>>
    >>>>"A" A wrote:
    >>>>>
    >>>>>If you have to read the whole file anyway, might as well read it all

    >in
    >>>>one
    >>>>>go. All those disk i/o calls can be expensive.
    >>>>>
    >>>>> Buffer=String(fileLen(FileName)," ")
    >>>>> Open FileName for Binary Access Read as #File
    >>>>> Get #File,,Buffer
    >>>>>
    >>>>>
    >>>>>
    >>>>>"Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >>>>>>
    >>>>>>Shawn,
    >>>>>>
    >>>>>>String functions are very slow in VB. In particular, when you're doing
    >>>>>lots
    >>>>>>of concatenation, VB is constantly reallocating and deallocating memory.
    >>>>>> One way to solve your problem is to pre-allocate the memory in advance.
    >>>>>> Most likely, this should solve your problem. In addition, you may

    >wish
    >>>>>>to change your function to a subroutine and pass the resulting string
    >>>by
    >>>>>>reference.
    >>>>>>
    >>>>>>Here are two modified versions of your routines. Just change the PATHNAME
    >>>>>>constant to whatever file you want to process.
    >>>>>>
    >>>>>>Private Const PATHNAME As String = "E:\CD-ROMs\VBCD\magazine\vbpjcode\1997\03mar97\PD0397P.zip"
    >>>>>>
    >>>>>>'as a function
    >>>>>>Public Function OpenWithString1() As String
    >>>>>> Dim strCharacter As String
    >>>>>> Dim bytVariable As Byte
    >>>>>> Dim strResult As String
    >>>>>> Dim intFileNbr As Integer
    >>>>>> Dim lngPos As Long
    >>>>>>
    >>>>>> strResult = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>>>> lngPos = 1
    >>>>>>
    >>>>>> intFileNbr = FreeFile
    >>>>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>>>
    >>>>>> Do While Not EOF(intFileNbr)
    >>>>>>
    >>>>>> Get #intFileNbr, , bytVariable
    >>>>>> strCharacter = Hex(bytVariable)
    >>>>>> If Len(strCharacter) = 1 Then
    >>>>>> strCharacter = "0" & strCharacter
    >>>>>> End If
    >>>>>> Mid$(strResult, lngPos, 2) = strCharacter
    >>>>>> lngPos = lngPos + 2
    >>>>>> Loop
    >>>>>> Close #intFileNbr
    >>>>>>
    >>>>>> OpenWithString1 = strResult
    >>>>>>
    >>>>>>End Function
    >>>>>>
    >>>>>>'as a subroutine with the resulting string being passed by reference
    >>>>>>Public Sub OpenWithString2(ByRef Result As String)
    >>>>>> Dim strCharacter As String
    >>>>>> Dim bytVariable As Byte
    >>>>>> Dim intFileNbr As Integer
    >>>>>> Dim lngPos As Long
    >>>>>>
    >>>>>> Result = "0x" & Space$(FileLen(PATHNAME) * 2)
    >>>>>> lngPos = 1
    >>>>>>
    >>>>>> intFileNbr = FreeFile
    >>>>>> Open PATHNAME For Binary Access Read As #intFileNbr
    >>>>>>
    >>>>>> Do While Not EOF(intFileNbr)
    >>>>>>
    >>>>>> Get #intFileNbr, , bytVariable
    >>>>>> strCharacter = Hex(bytVariable)
    >>>>>> If Len(strCharacter) = 1 Then
    >>>>>> strCharacter = "0" & strCharacter
    >>>>>> End If
    >>>>>> Mid$(Result, lngPos, 2) = strCharacter
    >>>>>> lngPos = lngPos + 2
    >>>>>> Loop
    >>>>>> Close #intFileNbr
    >>>>>>
    >>>>>>End Sub
    >>>>>>
    >>>>>>- Jim
    >>>>>>
    >>>>>>"Shawn Wilson" <wilso_s@mailcity.com> wrote:
    >>>>>>>
    >>>>>>>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
    >>>>>>
    >>>>>
    >>>>
    >>>

    >>

    >



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