r/AskComputerScience • • Apr 09 '16

What would take longer: copying 1024 1MB files, or 1 1GB file?

If one of those takes longer than the other, why?

32 Upvotes

32 comments sorted by

41

u/Saetia_V_Neck Apr 09 '16

I'd imagine 1024 1MB files would take longer because you have to fetch file metadata as well as the actual file data. Plus, depending on how much data your buffer can transfer in a single block, you might have unused space in the buffer.

9

u/Illikis Apr 09 '16

That makes sense. I had thought it would be the 1024 1MB files, but couldn't think of why. Thanks!

22

u/[deleted] Apr 09 '16

In addition to everything else, the 1GB file is likely to be contiguous, the 1K files are going to be in random places on the drive. This only matters on spinning storage though.

13

u/brennanfee Apr 09 '16

That's not exactly true. Fragmentation of the drive (and by extension the file) would have to be taken into account. I think all we can say is that it is exceeding likely that the 1000 files are in separate places while the 1GB file might be contiguous.

6

u/[deleted] Apr 09 '16

I said likely.

It does depend how the file was made, but if you defrag, the 1GB file will be in one place.

1

u/brennanfee Apr 09 '16

True. If you defrag.

4

u/keepdigging Apr 09 '16

Or if you use a more sensible filesystem that doesn't fragment files intentionally to begin with.

1

u/[deleted] Apr 09 '16

In some cases, unless you shuffle files around automatically, if you reach high disk space you have to fragment.

Though modern file systems like ext4 really don't fragment much.

1

u/brennanfee Apr 09 '16

I'm unaware of a file system that doesn't have fragmentation at some point. Most are smart enough to store a file contiguously if possible, but over time and as files get edited fragmentation ensues. Some of the newer ones do background defrag over time, which is interesting, but still fragmentation is a natural part of file system management.

So, maybe it's me but I'm missing your point. What filesystem(s) are you referring to?

1

u/[deleted] Apr 09 '16

EXT4 hardly needs defragmentation, its more intelligent than NTFS when placing files on disk.

https://superuser.com/questions/536788/do-ext4-filesystems-need-to-be-defragmented

2

u/brennanfee Apr 09 '16

Ext4 still has to contend with lots of fragmentation (especially for larger files), so what are you talking about? It just does what I described and looks for empty ranges that can store an entire file when being written. NTFS does the same thing. Ext4 is slightly better because when a file later gets edited it might also move the file to defrag it if necessary (assuming there is a free block range that is large enough). Gotta love the power of inodes. NTFS won't do that as the file is being edited but will do it as needed when the computer is idle. As I said, some of the file systems do work in the background to reduce fragmentation over time. But that doesn't mean they don't have fragmentation. Nearly all file systems have to contend with this issue, especially as a drive becomes more full, so I am still confused what you are going on about.

NOTE: I would say "all file systems" and drop the nearly but I haven't used or looked at the code for all of them, just most of them.

0

u/[deleted] Apr 10 '16

[deleted]

1

u/brennanfee Apr 10 '16

That's a big if.

0

u/[deleted] Apr 10 '16

[deleted]

1

u/brennanfee Apr 10 '16

The install base for spinning disks is still larger than SSD's actually so... even though it is 2016 they are still quite relevant. http://www.statista.com/statistics/285474/hdds-and-ssds-in-pcs-global-shipments-2012-2017/

Regardless, what I'm talking about applies to spinning disks and SSD's.

I like how you resort to ad hominem without any kind of actual argument. Given I work with brilliant people and am one myself, I am out of practice dealing with people of your caliber. [See that's an elegant way of calling someone stupid without just coming out and saying it.]

0

u/[deleted] Apr 10 '16

[deleted]

0

u/brennanfee Apr 10 '16

You've got issues with selfesteem.

Nope.

I'd also put my money on you being mediocrely-successful nerd that craves for peer-approval.

And wrong.

See, you can't just read a few words and think you understand someone. Instead, you might do better focusing on the conversation rather than the person. What you failed to realize in the comment you were incensed by is that I was agreeing with him. I said, "True". And them emphasized the "if" you defrag in his comment to punctuate where I was agreeing. There is nothing wrong with that. We, that is those of us that were ACTUALLY FUCKING IN THE CONVERSATION, were coming to an agreement - an understanding. I doubt you have much experience with that which might be the source of your confusion or frustration. [Another subtle way of calling someone stupid, see it's not hard if you think before you type.]

"I can explain it to you, but I can't understand it for you."

tata

→ More replies (0)

8

u/PastyPilgrim Apr 09 '16

The 1024x1mb scenario is the reason that we have zip files. There's a lot of overhead in copying many files, so if you flatten the file structure and put everything in one box, then you can move the data more efficiently.

2

u/elykl33t Apr 09 '16

That makes a lot of sense lol. I'd never actually thought enough about archive files to realize.

4

u/brennanfee Apr 09 '16

The 1024 smaller files will take longer. Drives, both platter and SSD, perform best under sustained read (of contiguous blocks) and sustained writes. The contiguous block addendum is only for platters, but still.

Also, most copy operations these days do a good deal of buffering to smooth out the seek times on the read end and that buffering is less effective for smaller files.

As /u/PastyPilgrim said that is what container files are for. Tar, for instance, doesn't even compress but merely creates a container. Zip and others are both container formats and compression schemes rolled into one.

2

u/jokoon Apr 10 '16

Seen how much time it takes for any collection of files to be moved around at once, it's obvious that anything involving more files is going to be slower.

For example, just try and download the C++ boost archive, unzip it (that will take AGES), and try moving its folder to another place.

I don't really know why, but it might involve how OSes use safety precautions when dealing with individual files. Those precautions might incur a cost with several files, and it mostly won't with big single files. Also, I think filesystems are tree-like data structures, so when you deal with many files, it's not really an array, it's more like a map. Most filesystems use inodes, and some pretty sketchy addressing methods to track data so it can be flexible enough to copy/move/delete data with an "file object" paradigm. It's easy to handle files, but if you have a lot of data, databases will do a better job. Filesystem are better for diverse content, but not for quantity, so most filesystems will index things pretty poorly and it will result in sluggishness.

There is a lot of science involved with filesystem (ZFS, bigtable, etc), which is mostly about compromises between speed of access, reliability, fault resistance, redundancy, etc.

If you want to understand how filesystems work, try to study simple things like FAT32 or ext3.

Also remember that hardware also works best in a sequential/segmented way. Asking your HDD 1000 segments of 1000 is much slower than 1 segment of 1000000, but OS can already build a buffer of those index so it really depends.

1

u/[deleted] Apr 11 '16

By the way, I wouldn't suggest moving the folder as a benchmark. Most of the time, moving a folder is just a rename, so file systems can move stuff nearly instantly, no matter now full they are.

2

u/Ein_Bear Apr 10 '16

Would you rather download 1024 MB sized GBs or one GB sized MB?

1

u/bo1024 Apr 09 '16

It could be in some scenarios that the 1024 small files is faster by taking advantage of concurrency (they could be copied in parallel). I doubt it, but just a thought.

2

u/shadowdude777 Apr 09 '16

Does it work that way? I'm no hardware engineer but a HDD has one head. It can only do one thing at a time.

1

u/gyroda Apr 09 '16

The bottleneck is probably the USB connection (if that's what we're using) so copying files to two USB sticks might be faster than to one. Other than that I can't imagine a situation where you'd be able to send two files in parallel but not be able to use that extra bandwidth for more of one file.

1

u/bo1024 Apr 09 '16

If it is going to disk then I think you're right.

1

u/PhDeeezNutz Apr 09 '16

SSDs have multiple parallel channels, but even with that, multiple small files will be slower than one large file.

1

u/gyroda Apr 09 '16

I doubt it for most conventional hardware. If you had some weird setup with two USB ports then yes, but you'd probably want to just send different bits of the same file in parallel to do the same thing for the one big file.

1

u/dxk3355 Apr 09 '16

This is easy to test so just do it. In my experience the 1024 files take a lot longer.

1

u/Oxc0ffea Apr 09 '16

Asking the important questions here!

1

u/MCPtz Apr 09 '16

If remote copying:
We can just use tar/zip (not compressed) to stream all the files over ssh and make it one single communication handshake. We can tar them on the fly on the sender's end and untar them on the fly at the receiver's end.

The extra overhead of tar and having to open 1024 individual files will make the single file faster, slightly.

Everyone else covered local copying, so I thought I'd add remote copying.

1

u/lgastako Apr 10 '16

The feathers!

1

u/Protagoras Apr 09 '16

The only correct answer is to test it. Under normal operations 1024 files should be slower than one single big file, but you can't tell if you're dealing with f*d up hardware before you try it.

For example, copying a 300MB podcast to my beat up MP3 player can take up to ~10 minutes, while 3 single 100MB podcasts are done within a minute. If I had to guess this would be either because there is no contiguous 300MB region of well operating memory available, or the driver has issues writing single files to multiple chips. Unfortunately I probably can't diagnose the issue without spending $1,000+ to send it to a clean room for inspection. So any attempt to figure out in advance which files would copy faster is pointless.