Results 1 to 9 of 9

Thread: Bash: timing a script

  1. #1
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Question Bash: timing a script

    Hi

    I need to benchmark a certain application and I want to use bash for this. I have already read that there is a /usr/bin/time program, apart from bash's own time command.

    I have two questions:
    1. /usr/bin/time outputs the "elapsed real time" and also the time spent by the program in user/kernel mode. Which of these is most relevant. If I am running other processes in the background, both elapsed time and user mode time are affected - there is no pure timing of a certain process
    2. /usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program


    Thank you all very much. PS: Still gaining experience in Ubuntu, so bare with me.
    Last edited by tg1w; November 30th, 2009 at 08:30 PM. Reason: typo

  2. #2
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: Bash: timing a script

    Maybe this link helps understanding te difference between kernel mode and user mode

  3. #3
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Lightbulb Re: Bash: timing a script

    Quote Originally Posted by Martin Witte View Post
    Maybe this link helps understanding te difference between kernel mode and user mode
    Thank you very much. It makes now sense why an application run in user mode, will also involve kernel mode operations.

    Would it sound reasonable then to time my simulation by summing the user mode and kernel mode times? Would it be more representative than just using the user mode?

    Thank you all.

  4. #4
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Question Re: Bash: timing a script

    /usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program

  5. #5
    Join Date
    May 2006
    Beans
    1,787

    Re: Bash: timing a script

    Quote Originally Posted by tg1w View Post
    /usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program
    For example:

    Code:
    $ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`
    There is also an --output option to 'time'. What do you want to happen if the program also outputs on stderr?

  6. #6
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Bash: timing a script

    Quote Originally Posted by Arndt View Post
    For example:

    Code:
    $ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`
    There is also an --output option to 'time'. What do you want to happen if the program also outputs on stderr?
    Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.

    My main goal is to execute a number of simulations, time each of them individually and then display the total/average time.

    I just read about times... maybe it would be better to use it, instead of doing arithmetical operations with time.

  7. #7
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Question Re: Bash: timing a script

    Quote Originally Posted by tg1w View Post
    Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.
    I wonder why using read doesn't solve the situation:

    Code:
    $ /usr/bin/time sleep 1 2>&1 | read -s sleep_time
    $ echo $sleep_time
    
    $

  8. #8
    Join Date
    May 2006
    Beans
    1,787

    Re: Bash: timing a script

    Quote Originally Posted by tg1w View Post
    Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.

    No, it does not. The backslash inhibits the builtin function. I just didn't want to spell out where the program is, even if it's unlikely to be somewhere else.

    With bash's builtin time function, it's harder to redirect its output, maybe not possible.

  9. #9
    Join Date
    Mar 2009
    Location
    York, UK
    Beans
    43
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Cool Re: Bash: timing a script

    [QUOTE=Arndt;8425395]For example:

    Code:
    $ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`
    Could you please help me out with the arithmetic operations too?

    If I want to get only the user mode seconds, the results will be something like
    Code:
    $ \time -f "%U" sleep 1 2>/tmp/timefile; execution_time=`cat /tmp/timefile`
    $ echo "$execution_time"
    0.00
    $
    That x.xx is anything but something that I can add then to other simulation times. Bash also doesn't support float numbers.

    Any help is welcomed. Thank you very much all.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •