bug-bash
[Top][All Lists]
Advanced

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

Re: Bug/limitation in 'time'


From: Hans J. Albertsson
Subject: Re: Bug/limitation in 'time'
Date: Sun, 17 Mar 2013 14:29:22 +0100
User-agent: Mozilla/5.0 (X11; SunOS i86pc; rv:17.0) Gecko/20130108 Thunderbird/17.0.2

This version is actually slightly faster... And the sys time goes down very close to zero.
Now, what were you actually looking to test??
My version might be utterly irrelevant.


function BashCount() {
        i=$1
        while (( i-- )) ; do
                 true
        done
        echo Just did $1 iterations using bash math
}
time BashCount 150000


On 2013-03-17 07:31, Pierre Gaston wrote:
On Sun, Mar 17, 2013 at 4:33 AM, Bruce Dawson
<address@hidden> wrote:
Thanks -- good to know that there is a fast and POSIX compliant method of
doing this. I should have included my optimized counting loop -- it's what
we switched to when we realized that $(expr) was a problem. Here it is now:

# This code performs quite well
function BashCount() {
         i=$1
         while [ $i -gt 0 ]; do
                 (( i-- ))
         done
         echo Just did $1 iterations using bash math
}
time BashCount 150000

It's a *lot* faster, of course. BTW, I've poked around in the 'time' source
code enough to know that it is just displaying the results of wait3(), so
the misleading CPU consumption information is ultimately a wait3()/kernel
issue. However showing this in the documentation would be great.
At least the man page of time on my ubuntu system is pretty much clear
about what it does.

The result is not striking me as impossible though, I can imagine a
lot of real time spent waiting for the scheduler to run expr and then
to run bash again.

I tried a little experiment that I think shows the importance of the
scheduler on the real time result:
I run at the same time this little loop with different "niceness"
i=0;time while ((i++<10000));do /bin/echo -n;done

sudo nice -n 19 bash -c 'i=0;time while ((i++<10000));do /bin/echo
-n;done' 2>&1| sed s/^/19:\ / &
sudo nice -n -20 bash -c 'i=0;time while ((i++<10000));do /bin/echo
-n;done' 2>&1| sed s/^/-20:\ /

I get:
-20: real       0m9.331s
-20: user       0m0.468s
-20: sys        0m1.504s

19: real        0m14.004s
19: user        0m0.532s
19: sys 0m1.660s

so the nicer loop takes twice as much real time indicating that much
real time is spent waiting for the process to run.




reply via email to

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