[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Why `$(<` is slow?
From: |
Eli Schwartz |
Subject: |
Re: [Help-bash] Why `$(<` is slow? |
Date: |
Fri, 30 Aug 2019 01:47:44 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 |
On 8/29/19 9:39 PM, Peng Yu wrote:
> 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?
Yes.
As you suspected, you already knew the answer.
> But shouldn't subshell be optimized away in such a case?
Why?
> 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
>
--
Eli Schwartz
Bug Wrangler and Trusted User
signature.asc
Description: OpenPGP digital signature