Find the answer to your Linux question:
Results 1 to 6 of 6
I have some slow code, that I want to run multiple times, and I would like to log the elapsed time to a file. Here is basically what I'm doing: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    1

    Simple BASH-script problem? v. Elapsed time


    I have some slow code, that I want to run multiple times, and I would like to log the elapsed time to a file. Here is basically what I'm doing:

    Code:
    before="$(date +%s)"
    long block of slow code
    after="$(date +%s)"
    elapsed_seconds="$(expr $after - $before)"
    echo Elapsed time: $(date -r $elapsed_seconds +%H:%M:%S)
    The problem is, a time interval of i.e. 3 seconds gives me 01:00:03 instead of 00:00:03

    Why the hell is that?

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    You shouldn't be using the -r flag, first of all. I'm not exactly sure how to use date to convert times like you want, but I do know that the -r flag takes seconds since the epoch as its argument. Do you happen to be time zone away from UTC time? That would explain the extra hour.

    So for example, I'm in EST. When I run
    Code:
    date -r 3
    I get Wed Dec 31 19:00:03 EST 1969, which is obviously wrong.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    tyskstil,

    I did a bit of thinking, and realized that you can just do some simple modular arithmetic. Here's a simple example, with the beginning based on your script:

    Code:
    #!/bin/bash
    
    before="$(date +%s)"
    ##########################
    #  Do lots of stuff here #
    ##########################
    after="$(date +%s)"
    elapsed="$(expr $after - $before)"
    elapsed=8923
    
    echo Elapsed secs: $elapsed
    
    remainder="$(expr $elapsed % 3600)"
    hours="$(expr $(expr $elapsed - $remainder) / 3600)"
    
    seconds="$(expr $remainder % 60)"
    minutes="$(expr $(expr $remainder - $seconds) / 60)"
    
    echo Elapsed time: $hours:$minutes:$seconds
    Of course, it's not as elegant as having something like date to use, but it's pretty simple and should work even for large values. Let me know if you do find a solution using date, though!
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    1

    Formatting seconds as hh:mm:ss

    To calculate elapsed time and format it into hh:mm:ss

    Ccde below will calculate elapsed time and format the elapsed time into hh:mm:ss correctly

    START_TIME=`date +%s`

    {some code}

    END_TIME=`date +%s`

    ELAPSED=`expr $END_TIME - $START_TIME`

    echo "FINISHED at " `date` " Elapsed time: " `date -d 00:00:$ELAPSED +%H:%M:%S`

  5. #5
    Just Joined!
    Join Date
    Jul 2007
    Posts
    1
    Ran this on RHEL 5.9 and it worked as long as ELAPSED < 60. With ELAPSED set to 104 I got this error.

    $ echo "FINISHED at " `date` " Elapsed time: " `date -d 00:00:$ELAPSED +%H:%M:%S`
    date: invalid date `00:00:104'

  6. #6
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,809
    Quote Originally Posted by tekberg View Post
    Ran this on RHEL 5.9 and it worked as long as ELAPSED < 60. With ELAPSED set to 104 I got this error.

    $ echo "FINISHED at " `date` " Elapsed time: " `date -d 00:00:$ELAPSED +%H:%M:%S`
    date: invalid date `00:00:104'
    I can't work out if you're contributing to the above (very old) discussion or if you have a new issue you want help with. It's the policy of is forum site to close down old thread like this if they get re-hashed as the advice often doesn't meet the current versions of the OS or tools (this one is nearly 5 years old), if you do have an issue then please start a new thread.and we'll take a look. I'm closing this one, though.
    Linux user #126863 - see http://linuxcounter.net/

Posting Permissions

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