On Tue, May 21, 2013 at 10:51 AM, Roland Mainz <[email protected]> wrote:
> Hi!
>
> ----
>
> Attached (as "astksh20130503_typeset_copy_operator001.diff.txt") is a
> small patch which provides a type-independent copy-by-name operator.
>
> Usage example:
> -- snip --
> # 1. copy float by name
> $ ksh -c 'float i=5.5 ; typeset -c j=i ; print $i $j'
> 5.5 5.5
>
> # 2. copy associative array by name
> $ ksh -c 'compound c=( typeset -A i=( [1]=4 [3]=5 [7]=6 ) ) ; typeset
> -c c.j=c.i ; print -v c'
> (
>         typeset -A i=(
>                 [1]=4
>                 [3]=5
>                 [7]=6
>         )
>         typeset -A j=(
>                 [1]=4
>                 [3]=5
>                 [7]=6
>         )
> )
> -- snip --
>
> The idea is to have a _generic_ and type-independent facility to
> _copy_ variables by _name_ (instead of j="$i" ... which is copy by
> value and doesn't work the same way for all objects).

copy-by-name instead of copy-by-value has major performance benefits
when ksh has to process very large strings or when an array has to be
filled with default values (let the value be a string or another
variable object/compound variable, it doesn't matter):

Copy-by-value takes 5 seconds here to complete:
> time ~/bin/ksh -c 'x="$(seq 50000)" ; typeset -a a ; for((i=0 ; i < 2000 ; 
> i++ )) ; do a[i]="$x" ; done ; #print -v a'

real    0m4.954s
user    0m4.705s
sys     0m0.231s

Same job using copy-by-name using Roland's patch takes less than half
the time to complete:
> time ~/bin/ksh -c 'x="$(seq 50000)" ; typeset -a a ; for((i=0 ; i < 2000 ; 
> i++ )) ; do typeset -c a[i]=x ; done ; #print -v a'

real    0m1.831s
user    0m1.563s
sys     0m0.260s

I like the patch :)

Irek
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers

Reply via email to