What is the difference? It is performed in the same?

tar -czf files/compressed/gzip/archive.tar.gz -C files/original .

tar -c -C files/original . | gzip -1 > files/compressed/gzip/archive.tar.gz
  • 1
    Your title asks about performance difference - did you measure the performance to see what happens in the real world? – Michael Kohne Jul 30 '15 at 13:50

In the former, the implementation of tar uses a gzip library to perform the compression itself. In the latter the output of tar gets piped into a separate executable that performs the compression. The former is probably slightly more efficient as it doesn't involve extra system calls to write/read the data to/from the pipe, but a human would likely not notice the difference.

  • At least GNU tar will call gzip itself, I don't think tar pipe to gzip is slower tar itself in this case. – cuonglm Jul 30 '15 at 17:08

Let's do some tests.

tar alone:

time sh -c 'tar -czf test.tar.gz ~/Downloads'
tar: Removing leading `/' from member names
JOB sh -c 'tar -czf test.tar.gz ~/Downloads'
74%    cpu
30.02s real
20.64s user
1.82s sys

tar pipe to gzip:

time sh -c 'tar -c ~/Downloads | gzip -1 > test.tar.gz'
tar: Removing leading `/' from member names
JOB sh -c 'tar -c ~/Downloads | gzip -1 > test.tar.gz'
65%    cpu
27.23s real
16.07s user
1.88s sys

It seems reasonable to me that the tar pipe to gzip can be faster because they were run in parallel in multiple core machine (mine has 2 CPUs).

  • Did you run both of these with a warm cache? – user253751 Jul 30 '15 at 19:36
  • I ran the same test with a warm cache, compressing from ramdisk to ramdisk, and found no difference between the two. – Mark Jul 30 '15 at 22:23

In the second option there are 2 applications that need to be started (tar and gzip). This takes extra time. Also the piping takes extra resources. This results in a longer execution time.

  • 1
    That's not necessarily true if tar and gzip are not running on the same CPU. But anyway the difference is likely to be microscopic in real-world scenarios. – Gilles 'SO- stop being evil' Jul 30 '15 at 23:17
  • I don't know how the shell handles the starting of gzip. Does it start gzip at the same time as tar or does it 'wait' until tar generates some output? But I agree with you that the difference in the start-up is very small in all cases. Not noticeable for the human eye. Think that the piping will cost more. – Marco Jul 31 '15 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.