How to time the duration of a script.

O

Oscar Gonzalez

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.
 
A

ara.t.howard

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
===============================================================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top