bug-bash
[Top][All Lists]
Advanced

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

Re: Unexpected delay in using arguments.


From: Bize Ma
Subject: Re: Unexpected delay in using arguments.
Date: Tue, 1 Jan 2019 22:57:37 -0400

Chet Ramey (<chet.ramey@case.edu>) wrote:

> It's in CWRU/CWRU.chlog in the development distributions
>

I am sorry but I have been unable to find either the source code
or the change log, either at the university site or by looking with google.

However, I have made some tests. Yes, the deeper internal delay
with arguments have been removed. Running this code
(from https://unix.stackexchange.com/q/462084/265604):

#!/bin/bash --
TIMEFORMAT='real: %R'
f1   () { :; }
f2   () { echo   '                args   = '"$#";
          printf '1 function no   args yes '; time for ((i=1;i<$n;i++));
do  :   ; done
          printf '2 function yes  args yes '; time for ((i=1;i<$n;i++));
do  f1  ; done
          set --
          printf '3 function yes  args no  '; time for ((i=1;i<$n;i++));
do  f1  ; done
          echo
        }
main () { set -- $(seq $m)
          f2  ""
          f2 "$@"
        }
n=1000; m=20000; main

Which had a 20 seconds delay on the second call with arguments (old
results):

                args   = 1
1 function no   args yes real:  0.012
2 function yes  args yes real:  0.033
3 function yes  args no  real:  0.024

                args   = 20000
1 function no   args yes real:  0.011
2 function yes  args yes real: 20.446
3 function yes  args no  real:  0.021

Has almost removed the *reported *delay (as ksh does):

b5b2sh ./script      # bash beta 2
                args   = 1
1 function no   args yes real: 0.026
2 function yes  args yes real: 0.053
3 function yes  args no  real: 0.051

                args   = 20000
1 function no   args yes real: 0.022
2 function yes  args yes real: 0.052
3 function yes  args no  real: 0.046

However, the delay is still there, just not reported.
Time the whole script to get:

time b5b2sh ./script
real    0m32.242s

The delay has actually increased to 32 seconds, while ksh is able to get:

time ksh ./script
real    0m0.137s

That's 32.242 / 0.137 = 235.343  times longer.

It seems that the delay has just moved from the deep internal
shuffling of arguments to the phases of creating
(and, now also, erasing) the arguments.

                              bash       b5b2sh        ksh
  set arguments              0.110       7.404        0.030
                args = 1
1 function no   args yes      0.014      0.025        0.020
2 function yes  args yes      0.027      0.055        0.020
         unset arguments      0.000      0.000        0.000
3 function yes  args no       0.024      0.053        0.020
  call function no args       0.080      5.207        0.070
                args = 20000
1 function no   args yes      0.011      0.026        0.000
2 function yes  args yes     20.430      0.055        0.010
         unset arguments      0.003      4.973        0.000
3 function yes  args no       0.020      0.048        0.010
  call function with args    20.578     23.530        0.040

total                        20.788s     41.128s    0.151

It seems odd that now calling the function without arguments
takes 5.207 seconds and that the un-setting (set --) the arguments
takes 4.973 seconds (setting a pointer to zero should be immediate).
While the call to the function is still taking (externally) ~20 seconds.

This test seems simpler, easier to run:

        time b5a0sh -c '
        TIMEFORMAT="real: %R";
        a=$(seq 20000);
        f2(){ :; };
        echo start>&2;
        time set -- $a;
        time f2 "$@";
        time set --'

Which yields:
  bash      b5b2sh     ksh
  0.097      8.154    0.020
  0.107     39.321    0.020
  0.003      5.470    0.000
  0.239s    52.975s   0.085s


reply via email to

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