what is faster, du or stat?

2

Which goes faster on Linux/Unix?

du -b file.dat

or:

stat -c %s file.dat

Matt

Posted 2011-07-17T23:47:47.417

Reputation: 627

1Isn't this something you could Just Try™? – jtbandes – 2011-07-18T00:05:05.500

I have tried it, and the results are sometimes inconsistent, that's why I'm asking other people. – Matt – 2011-07-18T16:48:26.437

Answers

3

It seems du is slightly faster in this example.

$ time bash -c 'for ((i=1; i<1000;i++)); do stat -c %s file1 >/dev/null; done'
real    0m3.588s
user    0m0.120s
sys 0m0.344s

$ time bash -c 'for ((i=1; i<1000;i++)); do du -b file1 >/dev/null; done'
real    0m3.161s
user    0m0.092s
sys 0m0.360s

But personally, I'd prefer to use stat, because most people use stat to retrieve basic information of a single file. So it's more likely stat will be optimized for such jobs in future.

Xiè Jìléi

Posted 2011-07-17T23:47:47.417

Reputation: 14 766

Here is stat: "real 0m2.183s" and here is du: "real 0m1.940s".. My file is 500MB [taken from /dev/zero], so I guess du is faster.. – Matt – 2011-07-18T16:49:50.870

3

You can use the time command to find out:

time du -b file.dat
time stat -c %s file.dat

Chris Acheson

Posted 2011-07-17T23:47:47.417

Reputation: 1 089