help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Why `$(<` is slow?


From: Peng Yu
Subject: [Help-bash] Why `$(<` is slow?
Date: Thu, 29 Aug 2019 20:39:15 -0500

I got the following runtime. `read` is the fastest (the last time statement).

But `$(<` is doing the same thing (the 4th time statement), shouldn't
it be as quick as read?

The assignment `=` takes little time (the 3rd time statement), so that
I think the most runtime is spent in `$(<` in the 4th time statement.

`$(<` is just a little faster than `$(cat`. Is I guess `$(<` involves
a subshell? But shouldn't subshell be optimized away in such a case?

Thanks.

$  ./main.sh
time for((i=0;i<1000;++i)); do ( cat "$tmpfile" ) done

real    0m1.112s
user    0m0.402s
sys     0m0.701s
time for((i=0;i<1000;++i)); do x=$(cat "$tmpfile"); done

real    0m1.218s
user    0m0.459s
sys     0m0.720s
time for((i=0;i<1000;++i)); do x=1; done

real    0m0.009s
user    0m0.008s
sys     0m0.001s
time for((i=0;i<1000;++i)); do x=$(< "$tmpfile"); done

real    0m1.169s
user    0m0.407s
sys     0m0.727s
time for((i=0;i<1000;++i)); do read -d '' -r x < "$tmpfile"; done

real    0m0.048s
user    0m0.027s
sys     0m0.020s
$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

tmpfile=$(mktemp)
enable -f cat cat
set -v
time for((i=0;i<1000;++i)); do ( cat "$tmpfile" ) done
time for((i=0;i<1000;++i)); do x=$(cat "$tmpfile"); done
time for((i=0;i<1000;++i)); do x=1; done
time for((i=0;i<1000;++i)); do x=$(< "$tmpfile"); done
time for((i=0;i<1000;++i)); do read -d '' -r x < "$tmpfile"; done

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]