Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to copy file much faster?

How to copy file much faster?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionlearning
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kcynic
    wrote on last edited by
    #1

    I have to copy files to movable devices using simple file operation such as fread and fwrite in a big loop. And I found some software do such works more quickly. I want to know if I used multithread it would be much faster?If it so,multithread would eate much CPU time resource.So,I think it perhaps no good to adding multithread to copy files. But I also want to know how the softwares can copy more fast? Thanks. GOOD LUCK.

    M M J 3 Replies Last reply
    0
    • K kcynic

      I have to copy files to movable devices using simple file operation such as fread and fwrite in a big loop. And I found some software do such works more quickly. I want to know if I used multithread it would be much faster?If it so,multithread would eate much CPU time resource.So,I think it perhaps no good to adding multithread to copy files. But I also want to know how the softwares can copy more fast? Thanks. GOOD LUCK.

      M Offline
      M Offline
      MarkusStr
      wrote on last edited by
      #2

      does your software run under a windows os? if so why don't you use the copyfile api from kernel32.dll? cheers markus

      K 1 Reply Last reply
      0
      • K kcynic

        I have to copy files to movable devices using simple file operation such as fread and fwrite in a big loop. And I found some software do such works more quickly. I want to know if I used multithread it would be much faster?If it so,multithread would eate much CPU time resource.So,I think it perhaps no good to adding multithread to copy files. But I also want to know how the softwares can copy more fast? Thanks. GOOD LUCK.

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        A friend of mine tried to write the fastest Win32 file copy a few years ago. He used 2 threads and 2 32K buffers (read one then switch and read other while writing the one ) and the Win32 API calls as opposed to the C Library file functions. I don't know if it was the best but it was 4 times faster than using the C library calls, mostly because they use small buffers, and 10 times faster than using Explorer on large files :wtf:

        Nothing is exactly what it seems but everything with seems can be unpicked.

        K 1 Reply Last reply
        0
        • K kcynic

          I have to copy files to movable devices using simple file operation such as fread and fwrite in a big loop. And I found some software do such works more quickly. I want to know if I used multithread it would be much faster?If it so,multithread would eate much CPU time resource.So,I think it perhaps no good to adding multithread to copy files. But I also want to know how the softwares can copy more fast? Thanks. GOOD LUCK.

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          If you're copying multiple files, you could spawn a thread for each file to copy so that, with respect to your program, the copying is taking place in parallel. Judy

          T K 2 Replies Last reply
          0
          • J JudyL_MD

            If you're copying multiple files, you could spawn a thread for each file to copy so that, with respect to your program, the copying is taking place in parallel. Judy

            T Offline
            T Offline
            Tydia kun
            wrote on last edited by
            #5

            There's just one problem here. First and foremost, it depends on WHERE and TO you copy. The HD is far slower than the processor and memory, so the bottleneck is THERE. Copying two files (or more) from the same HD and to the same HD, then it will slow down the operation at least 2x. If the source and destination is different for each file, then yes, you can use threads. Otherwise you're just slowing down things.

            J 1 Reply Last reply
            0
            • T Tydia kun

              There's just one problem here. First and foremost, it depends on WHERE and TO you copy. The HD is far slower than the processor and memory, so the bottleneck is THERE. Copying two files (or more) from the same HD and to the same HD, then it will slow down the operation at least 2x. If the source and destination is different for each file, then yes, you can use threads. Otherwise you're just slowing down things.

              J Offline
              J Offline
              JudyL_MD
              wrote on last edited by
              #6

              Depends on how the program is written. If it is a series of CopyFile, CopyFile, CopyFile functions calls, I agree 100% with you. If however, the OP is doing things between each CopyFile call, this may speed his process up by _always_ having data at the HD level waiting to be written while the OPs other code is executing. This is the kind of thing where you need to code it different ways and measure each ways performance in different source files / target disks scenarios. Sometimes you just have to say "sorry, hard drives are slow - you've just got to wait." That's why they made a wait cursor. Judy

              T 1 Reply Last reply
              0
              • J JudyL_MD

                Depends on how the program is written. If it is a series of CopyFile, CopyFile, CopyFile functions calls, I agree 100% with you. If however, the OP is doing things between each CopyFile call, this may speed his process up by _always_ having data at the HD level waiting to be written while the OPs other code is executing. This is the kind of thing where you need to code it different ways and measure each ways performance in different source files / target disks scenarios. Sometimes you just have to say "sorry, hard drives are slow - you've just got to wait." That's why they made a wait cursor. Judy

                T Offline
                T Offline
                Tydia kun
                wrote on last edited by
                #7

                Ah, of course. You're absolutely right. One thread can take care of copying and one thread can take care of everything else that needs to be done meanwhile... But then again, you don't need to use synchronus file copy either... You can asynchronous. While the file is copying, the program can do other stuff like readying more data copy or something, as long as it doesn't read or write to the same HD, since that would slow things down.

                1 Reply Last reply
                0
                • M MarkusStr

                  does your software run under a windows os? if so why don't you use the copyfile api from kernel32.dll? cheers markus

                  K Offline
                  K Offline
                  kcynic
                  wrote on last edited by
                  #8

                  I follow your guide,using CopyFile() first and using C standard io fuctions( using a 512 bytes buffer) at second.The file is 673298KB.The result is: CopyFile: 73 sec C Standard: 49 sec And how it do copying work of kernel32.dll(CopyFile)?

                  1 Reply Last reply
                  0
                  • M Matthew Faithfull

                    A friend of mine tried to write the fastest Win32 file copy a few years ago. He used 2 threads and 2 32K buffers (read one then switch and read other while writing the one ) and the Win32 API calls as opposed to the C Library file functions. I don't know if it was the best but it was 4 times faster than using the C library calls, mostly because they use small buffers, and 10 times faster than using Explorer on large files :wtf:

                    Nothing is exactly what it seems but everything with seems can be unpicked.

                    K Offline
                    K Offline
                    kcynic
                    wrote on last edited by
                    #9

                    Thank you very much. I will have a try this way,soon.

                    1 Reply Last reply
                    0
                    • J JudyL_MD

                      If you're copying multiple files, you could spawn a thread for each file to copy so that, with respect to your program, the copying is taking place in parallel. Judy

                      K Offline
                      K Offline
                      kcynic
                      wrote on last edited by
                      #10

                      In fact,the biggest problem is how to copy large file faster?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups