0

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
  • 1
    Your title asks about performance difference - did you measure the performance to see what happens in the real world? Jul 30, 2015 at 13:50

3 Answers 3

6

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).

2
  • Did you run both of these with a warm cache?
    – user253751
    Jul 30, 2015 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, 2015 at 22:23
4

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.

1
  • 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, 2015 at 17:08
1

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.

2
  • 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. Jul 30, 2015 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, 2015 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .