How to time the duration of a script.

Discussion in 'Ruby' started by Oscar Gonzalez, Dec 12, 2005.

  1. If I wanted to find something like the "time" feature on shell...

    where you can do:

    time ls

    and it will tell you how long it took to execute...

    How would I do this in Ruby or what is the packaged that can do this for
    me? I want it to kick off as soon as the script gets called and just
    print the time taken to execute at the end of the script.

    --
    Posted via http://www.ruby-forum.com/.
    Oscar Gonzalez, Dec 12, 2005
    #1
    1. Advertising

  2. Oscar Gonzalez

    Guest

    On Tue, 13 Dec 2005, Oscar Gonzalez wrote:

    > If I wanted to find something like the "time" feature on shell...
    >
    > where you can do:
    >
    > time ls
    >
    > and it will tell you how long it took to execute...
    >
    > How would I do this in Ruby or what is the packaged that can do this for
    > me? I want it to kick off as soon as the script gets called and just
    > print the time taken to execute at the end of the script.


    harp:~ > cat elapsed.rb
    BEGIN{ $start_time = Time::now.to_f }
    END{ $end_time = Time::now.to_f; puts "#{ $0 } elapsed: #{ $end_time - $start_time} }" }

    harp:~ > cat a.rb
    sleep 2 and p 42

    harp:~ > ruby -r elapsed a.rb
    42
    a.rb elapsed: 1.99891686439514 }

    or

    harp:~ > cat a.rb
    #!/usr/bin/env ruby
    sleep 2 and p 42

    harp:~ > RUBYOPT="-relapsed" ./a.rb
    42
    ./a.rb elapsed: 1.99908494949341 }

    hth.

    -a
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
    , Dec 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CJ
    Replies:
    2
    Views:
    581
  2. Replies:
    9
    Views:
    377
  3. rainny

    Count the time duration

    rainny, Mar 23, 2009, in forum: Java
    Replies:
    13
    Views:
    2,604
  4. dave
    Replies:
    3
    Views:
    517
    Gabriel Genellina
    Jun 5, 2010
  5. Kris Rudin

    Formatting time as a duration?

    Kris Rudin, Nov 10, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    106
    Michael Tkachev
    Nov 13, 2003
Loading...

Share This Page