help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] slow in function creation


From: Peng Yu
Subject: Re: [Help-bash] slow in function creation
Date: Mon, 3 Dec 2018 22:58:51 -0600

On Mon, Dec 3, 2018 at 10:26 PM Peng Yu <address@hidden> wrote:
>
> Hi,
>
> I see that function creation can be much slower than typeset -F when
> there are many functions. Why function creation can be so slow? Is
> there a way to improve its runtime?
>
> I see that the typeset -F slows down ~10x from 100000 to 1000000. Is
> 100000 approximately the bucket size of the hash storing the
> functions?
>
> $ ./main.sh
> ==> 1 <==
> 0.008
> 0.000
> 1.254
> ==> 10 <==
> 0.009
> 0.000
> 1.291
> ==> 1000 <==
> 0.013
> 0.007
> 1.280
> ==> 10000 <==
> 0.022
> 0.065
> 1.296
> ==> 100000 <==
> 0.110
> 1.706
> 1.767
> ==> 1000000 <==
> 1.015
> 218.159
> 10.999
> $ cat main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> tmpfile=$(mktemp -u)
> TIMEFORMAT=%R
> for n in 1 10 1000 10000 100000 1000000
> do
>     echo "==> $n <=="
>     time awk -v n="$n" -e 'BEGIN { for(i=1;i<n;++i) printf("function
> f%d { :; }\n", i) }' > "$tmpfile"
>     time source "$tmpfile"
>     time for ((i=0;i<100000;++i))
>     do
>         typeset -F f0
>     done > /dev/null
> done

An associative array has about the same runtime characteristics. I
assume that the function table and associative uses the same
underlying hash implementation? Is it so?

$  ./main.sh
==> 1 <==
0.008
0.000
1.211
==> 10 <==
0.010
0.000
1.220
==> 100 <==
0.009
0.001
1.200
==> 1000 <==
0.012
0.006
1.191
==> 10000 <==
0.022
0.065
1.275
==> 100000 <==
0.106
1.996
2.092
==> 1000000 <==
1.186
336.367
15.847

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

#set -v
tmpfile=$(mktemp -u)
TIMEFORMAT=%R
for n in 1 10 100 1000 10000 100000 1000000
do
    echo "==> $n <=="
    declare -A x
    time awk -v n="$n" -e 'BEGIN { for(i=0;i<n;++i)
printf("x[x%d]=\n", i ) }' > "$tmpfile"
    time source "$tmpfile"
    time for ((i=0;i<100000;++i))
    do
        [[ -v x[x0] ]]
    done > /dev/null
    unset x
done

-- 
Regards,
Peng



reply via email to

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