How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J French

    #16
    Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

    On 9 Jan 2004 15:53:14 -0800, bwr@unicon.com (Bryan Rickard) wrote:
    [color=blue]
    >Steve, thank you very much! That took care of the problem.
    >
    >I wonder, though, why VB doesn't reallocate the same memory when you
    >redefine a fixed-length string. You would think it would.[/color]

    I agree, but even so it would still be quite a lot of unnecessary work
    for the App.

    Also, did you get my point about using Byte Arrays
    - when VB reads data into a String it converts single bytes into 2
    byte Unicode
    - and when it writes it, it converts 2 byte Unicode to single bytes

    Comment

    • Raoul Watson

      #17
      Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


      "Bryan Rickard" <bwr@unicon.com > wrote in message
      news:2b02b188.0 401071806.1337d 16d@posting.goo gle.com...[color=blue]
      > I wrote a simple program in VB6 to copy all the files from a directory
      > on a CD-ROM to my hard disk. There are about 10 files, each about
      > 30MB.
      >
      > The program uses Get and Put to get data from the CD into a buffer and
      > then put it into the disk. See code below. It works, but it slows
      > down drastically before it copies all the files. Windows Task Manager
      > shows the CPU usage gradually increasing as the files are copied,
      > until it reaches 100 percent after about 6 files. That's when it
      > starts to get slow.
      >
      > How can I prevent the CPU usage increasing? Here's the guts of the
      > code. All this is within another loop that goes through all the files
      > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
      > million, makes no difference.
      >
      > iCDFN = FreeFile
      > Open sCDPath & sFileName For Binary Access Read As #iCDFN
      > iDiskFN = FreeFile
      > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
      > Do
      > If lStartPos + lIncrement > lFileLen Then _
      > lIncrement = lFileLen - lStartPos + 1
      > sBuffer = String(lIncreme nt, " ")
      > Get #iCDFN, , sBuffer
      > Put #iDiskFN, , sBuffer
      > lStartPos = lStartPos + lIncrement
      > If lStartPos > lFileLen Then Exit Do
      > Loop
      > Close iCDFN
      > Close iDiskFN
      >
      > TIA - Bryan Rickard[/color]

      Bryan..

      I know this doesn't address your question but just out of curiosity why
      don't you just shell out? The operating system has optimized file copying
      functions that I think we should make use of instead of bloating our code /
      re-inventing the wheel.

      You can either shell out, use API, or the file system object.


      Comment

      • Charles Kincaid

        #18
        Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

        bwr@unicon.com (Bryan Rickard) wrote in
        news:2b02b188.0 401091552.ecec8 35@posting.goog le.com:
        [color=blue]
        > Steve, thank you very much! That took care of the problem.
        >
        > I wonder, though, why VB doesn't reallocate the same memory when you
        > redefine a fixed-length string. You would think it would.
        >
        > - Bryan[/color]

        Because it is not a fixed-length string. String returns a variant
        containing a variable length string of the length specified containing
        the specified byte. Please read that carfully. String returns a variant
        while String$ returns a string. Variant type conversions can be very
        slow in VB. So if sBuffer is declared a string (you do use Option
        Explicit and correctly type all of your variables, right?) then sBuffer =
        String(lIncreme nt," ") where lIncrement=3 million creates a 3 million
        byte string in a variant and then copies that to a 3 million byte string.

        Steve Gerrard is also right when he says build the string buffer only
        when the buffer size changes. As far as writing the little piece first I
        gave that up some years back as I realized that by doing so I avoid any
        benefits of trying to help the OS by picking the right size buffer.

        A far as lIncrement size: Pick an exact multiple of the destination
        volume cluster size. Do keep it reasonably small. If there are not 3
        miilon free bytes in the pool then a 3 meg string is going to get
        virtualized. Less likely to happen with lengths of 64 K or so.

        --
        ATB

        Charles Kincaid

        Comment

        • Bryan Rickard

          #19
          Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

          "Raoul Watson" <WatsonR@Intell igenCIA.com> wrote in message news:<n1gMb.555 5$Qq.4640@nwrdn y01.gnilink.net >...[color=blue]
          >
          > Bryan..
          >
          > I know this doesn't address your question but just out of curiosity why
          > don't you just shell out? The operating system has optimized file copying
          > functions that I think we should make use of instead of bloating our code /
          > re-inventing the wheel.
          >
          > You can either shell out, use API, or the file system object.[/color]

          Raoul - do you mean something like RetVal = Shell("explorer .exe", 1)?
          If so, I need something more customized for this application, at the
          very least it would need to open with the CD drive contents showing,
          but a new Explorer window always opens at My Documents (annoyingly).
          Thanks for the input anyway, I might find a need for that one day, and
          I hadn't thought of it.

          - Bryan

          Comment

          • Raoul Watson

            #20
            Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


            "Bryan Rickard" <bwr@unicon.com > wrote in message
            news:2b02b188.0 401131659.653d9 999@posting.goo gle.com...[color=blue]
            > "Raoul Watson" <WatsonR@Intell igenCIA.com> wrote in message[/color]
            news:<n1gMb.555 5$Qq.4640@nwrdn y01.gnilink.net >...[color=blue][color=green]
            > >
            > > Bryan..
            > >
            > > I know this doesn't address your question but just out of curiosity why
            > > don't you just shell out? The operating system has optimized file[/color][/color]
            copying[color=blue][color=green]
            > > functions that I think we should make use of instead of bloating our[/color][/color]
            code /[color=blue][color=green]
            > > re-inventing the wheel.
            > >
            > > You can either shell out, use API, or the file system object.[/color]
            >
            > Raoul - do you mean something like RetVal = Shell("explorer .exe", 1)?
            > If so, I need something more customized for this application, at the
            > very least it would need to open with the CD drive contents showing,
            > but a new Explorer window always opens at My Documents (annoyingly).
            > Thanks for the input anyway, I might find a need for that one day, and
            > I hadn't thought of it.
            >
            > - Bryan[/color]

            Not really. What I mean is have a file selection dialog and once the source
            file is identified use Windows API or the file system object to copy it or
            simple shell out to a batch file like Shell "copy.bat" where copy.bat would
            have copy d:\whateverdir\ whateverfile c:\whereever

            If you need it, give me a holler, I'll e-mail you.


            Comment

            • Raoul Watson

              #21
              Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


              "Bryan Rickard" <bwr@unicon.com > wrote in message
              news:2b02b188.0 401131659.653d9 999@posting.goo gle.com...[color=blue]
              > "Raoul Watson" <WatsonR@Intell igenCIA.com> wrote in message[/color]
              news:<n1gMb.555 5$Qq.4640@nwrdn y01.gnilink.net >...[color=blue][color=green]
              > >
              > > Bryan..
              > >
              > > I know this doesn't address your question but just out of curiosity why
              > > don't you just shell out? The operating system has optimized file[/color][/color]
              copying[color=blue][color=green]
              > > functions that I think we should make use of instead of bloating our[/color][/color]
              code /[color=blue][color=green]
              > > re-inventing the wheel.
              > >
              > > You can either shell out, use API, or the file system object.[/color]
              >
              > Raoul - do you mean something like RetVal = Shell("explorer .exe", 1)?
              > If so, I need something more customized for this application, at the
              > very least it would need to open with the CD drive contents showing,
              > but a new Explorer window always opens at My Documents (annoyingly).
              > Thanks for the input anyway, I might find a need for that one day, and
              > I hadn't thought of it.
              >
              > - Bryan[/color]

              Here is one quick way..
              Dim SourceF, DestF
              SourceF = "D:\WHATEVERDIR \MYFILE.DAT" ' Define source file name.
              DestF = "C:\WHEREVER\MY FILE.DAT" ' Define target file name.
              FileCopy SourceF, DestF ' Copy source to target.



              Comment

              • Bryan Rickard

                #22
                Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                erewhon@nowhere .com (J French) wrote in message news:<3fffcecf. 169051856@news. btclick.com>...[color=blue]
                > Also, did you get my point about using Byte Arrays
                > - when VB reads data into a String it converts single bytes into 2
                > byte Unicode
                > - and when it writes it, it converts 2 byte Unicode to single bytes[/color]

                I tried using Byte Arrays, they work fine with fixed length arrays but
                for the left-over bytes I had to use a dynamic array, and the Put
                statement added an extra string of hundreds of nulls (hex 0) onto the
                end of the left-over bytes. Here's the code I used:

                Dim bBytes(65536) As Byte
                Dim bLeftOverBytes( ) As Byte
                ...
                Do
                If lStartPos + lIncrement > lFileLen Then
                lLeftOver = lFileLen - lStartPos + 1
                ReDim bLeftOverBytes( lLeftOver)
                Get #iCDFN, , bLeftOverBytes
                Put #iDiskFN, , bLeftOverBytes 'writes too many bytes
                Exit Do
                Else 'this is the normal loop, it works fine
                lStartPos = lStartPos + lIncrement
                Get #iCDFN, , bBytes
                Put #iDiskFN, , bBytes
                End If
                Loop

                Any ideas?

                Comment

                • Bryan Rickard

                  #23
                  Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                  Charles Kincaid <kincaic@swbell .net> wrote in message news:<Xns946F6D 78DA5D7kincaics wbellnet@38.144 .126.67>...[color=blue]
                  >
                  > Because it is not a fixed-length string. String returns a variant
                  > containing a variable length string of the length specified containing
                  > the specified byte. Please read that carfully. String returns a variant
                  > while String$ returns a string. Variant type conversions can be very
                  > slow in VB. So if sBuffer is declared a string (you do use Option
                  > Explicit and correctly type all of your variables, right?) then sBuffer =
                  > String(lIncreme nt," ") where lIncrement=3 million creates a 3 million
                  > byte string in a variant and then copies that to a 3 million byte string.
                  >
                  > Steve Gerrard is also right when he says build the string buffer only
                  > when the buffer size changes. As far as writing the little piece first I
                  > gave that up some years back as I realized that by doing so I avoid any
                  > benefits of trying to help the OS by picking the right size buffer.
                  >
                  > A far as lIncrement size: Pick an exact multiple of the destination
                  > volume cluster size. Do keep it reasonably small. If there are not 3
                  > miilon free bytes in the pool then a 3 meg string is going to get
                  > virtualized. Less likely to happen with lengths of 64 K or so.[/color]

                  Thank you, Charles, good inputs.

                  Comment

                  Working...