# Calculating elapsed time

**URL:** <https://rubytalk.org/t/calculating-elapsed-time/45916>\
**Category:** ruby-talk\
**Created:** [16 April 2008 09:56 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916 "2008-04-16T09:56:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clement\_Ow](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clement\_Ow](https://rubytalk.org/u/Clement_Ow)\
**Post date:** [16 April 2008 09:56 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/1 "2008-04-16T09:56:04Z")

</div>

I have a program where i put the time at the beginning:  
puts $t1=Time.now

and then after running some commands, i put the time again:  
puts $t2=Time.now

and then have a method to calculate the time elapsed:  
def calTime  
starttime\_sec= $t1.strftime("%S").to\_i  
starttime\_min= $t1.strftime("%M").to\_i  
endtime\_sec= $t2.strftime("%S").to\_i  
endtime\_min= $t2.strftime("%M").to\_i  
diff\_sec= endtime\_sec - starttime\_sec  
diff\_sec1= diff\_sec.to\_s  
diff\_min= endtime\_min - starttime\_min  
diff\_min1= diff\_min.to\_s  
puts "\nElapsed time:\n "+ "Min-\> " +diff\_min1 + " Sec-\> "+ diff\_sec1

However, the calculation has limitations because if the start time is  
say, 4:50 and the end time is 5:10, it will return an errorneous value  
like -49 for the Min value, which is obviously not what we want..

Any alternative method or any corrections to the method are welcome. 😉

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [16 April 2008 10:04 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/2 "2008-04-16T10:04:53Z")

</div>

require 'benchmark'

time = Benchmark.realtime {  
do\_all\_stuff  
}

puts "Elapsed time: #{time}"

😉

> **···**
>
> 2008/4/16, Clement Ow \<clement.ow@asia.bnpparibas.com\>:
> 
> > Any alternative method or any corrections to the method are welcome. 😉
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [16 April 2008 10:24 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/3 "2008-04-16T10:24:56Z")

</div>

Alternative:

irb(main):001:0\> start = Time.now  
=\> Wed Apr 16 12:06:23 +0200 2008  
irb(main):003:0\> finish = Time.now  
=\> Wed Apr 16 12:07:24 +0200 2008  
irb(main):005:0\> elapsed = (finish - start).to\_i  
=\> 65  
irb(main):012:0\> mins = elapsed / 60  
=\> 1  
irb(main):013:0\> secs = elapsed % 60  
=\> 5

Jesus.

> **···**
>
> On Wed, Apr 16, 2008 at 11:56 AM, Clement Ow \<clement.ow@asia.bnpparibas.com\> wrote:
> 
> > I have a program where i put the time at the beginning:  
> > puts $t1=Time.now
> > 
> > and then after running some commands, i put the time again:  
> > puts $t2=Time.now
> > 
> > and then have a method to calculate the time elapsed:  
> > def calTime  
> > starttime\_sec= $t1.strftime("%S").to\_i  
> > starttime\_min= $t1.strftime("%M").to\_i  
> > endtime\_sec= $t2.strftime("%S").to\_i  
> > endtime\_min= $t2.strftime("%M").to\_i  
> > diff\_sec= endtime\_sec - starttime\_sec  
> > diff\_sec1= diff\_sec.to\_s  
> > diff\_min= endtime\_min - starttime\_min  
> > diff\_min1= diff\_min.to\_s  
> > puts "\nElapsed time:\n "+ "Min-\> " +diff\_min1 + " Sec-\> "+ diff\_sec1
> > 
> > However, the calculation has limitations because if the start time is  
> > say, 4:50 and the end time is 5:10, it will return an errorneous value  
> > like -49 for the Min value, which is obviously not what we want..
> > 
> > Any alternative method or any corrections to the method are welcome. 😉

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [16 April 2008 14:47 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/4 "2008-04-16T14:47:05Z")

</div>

> > I have a program where i put the time at the beginning:  
> > puts $t1=Time.now
> > 
> > and then after running some commands, i put the time again:  
> > puts $t2=Time.now
> > 
> > and then have a method to calculate the time elapsed:  
> > def calTime  
> > starttime\_sec= $t1.strftime("%S").to\_i  
> > starttime\_min= $t1.strftime("%M").to\_i  
> > endtime\_sec= $t2.strftime("%S").to\_i  
> > endtime\_min= $t2.strftime("%M").to\_i  
> > diff\_sec= endtime\_sec - starttime\_sec  
> > diff\_sec1= diff\_sec.to\_s  
> > diff\_min= endtime\_min - starttime\_min  
> > diff\_min1= diff\_min.to\_s  
> > puts "\nElapsed time:\n "+ "Min-\> " +diff\_min1 + " Sec-\> "+ diff\_sec1
> > 
> > However, the calculation has limitations because if the start time is  
> > say, 4:50 and the end time is 5:10, it will return an errorneous value  
> > like -49 for the Min value, which is obviously not what we want..
> > 
> > Any alternative method or any corrections to the method are welcome. 😉
> 
> Alternative:
> 
> irb(main):001:0\> start = Time.now  
> =\> Wed Apr 16 12:06:23 +0200 2008  
> irb(main):003:0\> finish = Time.now  
> =\> Wed Apr 16 12:07:24 +0200 2008  
> irb(main):005:0\> elapsed = (finish - start).to\_i  
> =\> 65  
> irb(main):012:0\> mins = elapsed / 60  
> =\> 1  
> irb(main):013:0\> secs = elapsed % 60  
> =\> 5
> 
> Jesus.

or:

> start = Time.now

=\> Wed Apr 16 10:41:03 -0400 2008

> finish = Time.now

=\> Wed Apr 16 10:45:53 -0400 2008

> elapsed = finish.to\_f - start.to\_f # note to\_f to keep

fractional seconds  
=\> 289.695813894272

> mins, secs = elapsed.divmod 60.0

=\> [4, 49.6958138942719]

> puts("%3d:%04.2f"%[mins.to\_i, secs])

&nbsp;&nbsp;&nbsp;4:49.70  
=\> nil

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Apr 16, 2008, at 6:24 AM, Jesús Gabriel y Galán wrote:
> 
> > On Wed, Apr 16, 2008 at 11:56 AM, Clement Ow \> \<clement.ow@asia.bnpparibas.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [16 April 2008 15:01 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/5 "2008-04-16T15:01:42Z")

</div>

> or:  
> \> start = Time.now  
> =\> Wed Apr 16 10:41:03 -0400 2008  
> \> finish = Time.now  
> =\> Wed Apr 16 10:45:53 -0400 2008  
> \> elapsed = finish.to\_f - start.to\_f # note to\_f to keep fractional  
> seconds

Yep, I assumed he wanted to stop at the seconds level.

> =\> 289.695813894272  
> \> mins, secs = elapsed.divmod 60.0  
> =\> [4, 49.6958138942719]  
> \> puts("%3d:%04.2f"%[mins.to\_i, secs])  
> &nbsp;&nbsp;4:49.70  
> =\> nil

I always forget about divmod, it's a cool little method 🙂

Jesus.

> **···**
>
> On Wed, Apr 16, 2008 at 4:47 PM, Rob Biedenharn \<Rob@agileconsultingllc.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Clement\_Ow](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clement\_Ow](https://rubytalk.org/u/Clement_Ow)\
**Post date:** [17 April 2008 01:38 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/6 "2008-04-17T01:38:44Z")

</div>

Thanks guys of the prompt reply! However, i'll need help the  
explanations though.. Really appreciate that 😉

Jesús Gabriel y Galán wrote:

> > or:  
> > \> start = Time.now  
> > =\> Wed Apr 16 10:41:03 -0400 2008  
> > \> finish = Time.now  
> > =\> Wed Apr 16 10:45:53 -0400 2008  
> > \> elapsed = finish.to\_f - start.to\_f # note to\_f to keep fractional  
> > seconds
> 
> Yep, I assumed he wanted to stop at the seconds level.
> 
> > =\> 289.695813894272  
> > \> mins, secs = elapsed.divmod 60.0  
> > =\> [4, 49.6958138942719]  
> > \> puts("%3d:%04.2f"%[mins.to\_i, secs])

how this line work? "%3d" and %04.2f" means selecting min and secs  
respectively?

> **···**
>
> > On Wed, Apr 16, 2008 at 4:47 PM, Rob Biedenharn \> \<Rob@agileconsultingllc.com\> wrote:
> > 
> > > &nbsp;&nbsp;4:49.70  
> > > =\> nil
> > 
> > I always forget about divmod, it's a cool little method 🙂
> > 
> > Jesus.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Daniel\_Finnie](https://rubytalk.org/u/Daniel_Finnie)\
**Post date:** [17 April 2008 02:20 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/7 "2008-04-17T02:20:38Z")

</div>

Hi,

Ruby substitutes the elements in the array for the percent signs in  
the string. The "3d" and "04.2f" tell Ruby how to format those  
numbers:

> > "The number is: %.2f" % 1.23456

=\> "The number is: 1.23"

The documentation for sprintf has more info:  
daniel@daniel-desktop:~/lightconsole$ ri sprintf  
--------------------------------------------------------- Kernel#sprintf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format(format\_string [, arguments...] ) =\> string  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(format\_string [, arguments...] ) =\> string

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the string resulting from applying format\_string to any  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;additional arguments. Within the format string, any characters  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other than format sequences are copied to the result. A format  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence consists of a percent sign, followed by optional flags,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width, and precision indicators, then terminated with a field type  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;character. The field type controls how the corresponding sprintf  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argument is to be interpreted, while the flags modify that  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpretation. The field type characters are listed in the table  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at the end of this section. The flag characters are:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flag | Applies to | Meaning  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;space | bdeEfgGiouxX | Leave a space at the start of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> positive numbers.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(digit)$ | all | Specifies the absolute argument number  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> for this field. Absolute and relative  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> argument numbers cannot be mixed in a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> sprintf string.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# | beEfgGoxX | Use an alternative format. For the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> conversions `o', `x', `X', and `b',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> prefix the result with ``0'', ``0x'', ``0X'',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> and ``0b'', respectively. For `e',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> `E', `f', `g', and 'G', force a decimal  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> point to be added, even if no digits follow.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> For `g' and 'G', do not remove trailing zeros.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ | bdeEfgGiouxX | Add a leading plus sign to positive numbers.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- | all | Left-justify the result of this conversion.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 (zero) | bdeEfgGiouxX | Pad with zeros, not spaces.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------+--------------+-----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* | all | Use the next argument as the field width.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> If negative, left-justify the result. If the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> asterisk is followed by a number and a dollar  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> \> sign, use the indicated argument as the width.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The field width is an optional integer, followed optionally by a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;period and a precision. The width specifies the minimum number of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;characters that will be written to the result for this field. For  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numeric fields, the precision controls the number of decimal  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;places displayed. For string fields, the precision determines the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maximum number of characters to be copied from the string. (Thus,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the format sequence %10.10s will always contribute exactly ten  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;characters to the result.)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The field types are:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Field | Conversion  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;------+--------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b | Convert argument as a binary number.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c | Argument is the numeric code for a single character.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d | Convert argument as a decimal number.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;E | Equivalent to `e', but uses an uppercase E to indicate  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> the exponent.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e | Convert floating point argument into exponential notation  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> with one digit before the decimal point. The precision  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> determines the number of fractional digits (defaulting to six).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f | Convert floating point argument as [-]ddd.ddd,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> where the precision determines the number of digits after  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> the decimal point.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;G | Equivalent to `g', but use an uppercase `E' in exponent form.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g | Convert a floating point number using exponential form  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> if the exponent is less than -4 or greater than or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> equal to the precision, or in d.dddd form otherwise.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i | Identical to `d'.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o | Convert argument as an octal number.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p | The valuing of argument.inspect.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s | Argument is a string to be substituted. If the format  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> sequence contains a precision, at most that many characters  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> will be copied.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u | Treat argument as an unsigned decimal number. Negative integers  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> are displayed as a 32 bit two's complement plus one for the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> underlying architecture; that is, 2 \*\* 32 + n. However, since  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> Ruby has no inherent limit on bits used to represent the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> integer, this value is preceded by two dots (..) in order to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> indicate a infinite number of leading sign bits.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X | Convert argument as a hexadecimal number using uppercase  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> letters. Negative numbers will be displayed with two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> leading periods (representing an infinite string of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> leading 'FF's.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x | Convert argument as a hexadecimal number.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> Negative numbers will be displayed with two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> leading periods (representing an infinite string of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> leading 'ff's.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Examples:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%d %04x", 123, 123) #=\> "123 007b"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%08b '%4s'", 123, 123) #=\> "01111011 ' 123'"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%1$\*2$s %2$d %1$s", "hello", 8) #=\> " hello 8 hello"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%1$\*2$s %2$d", "hello", -8) #=\> "hello -8"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=\> "+1.23: 1.23:1.23"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%u", -123) #=\> "..4294967173"
> 
> Dan
> 
> On Wed, Apr 16, 2008 at 9:38 PM, Clement Ow \<clement.ow@asia.bnpparibas.com\> wrote:
> 
> > Thanks guys of the prompt reply! However, i'll need help the  
> > explanations though.. Really appreciate that 😉
> > 
> > Jesús Gabriel y Galán wrote:
> > 
> > \> On Wed, Apr 16, 2008 at 4:47 PM, Rob Biedenharn \> \> \<Rob@agileconsultingllc.com\> wrote:  
> > \>  
> > \>\> or:  
> > \>\> \> start = Time.now  
> > \>\> =\> Wed Apr 16 10:41:03 -0400 2008  
> > \>\> \> finish = Time.now  
> > \>\> =\> Wed Apr 16 10:45:53 -0400 2008  
> > \>\> \> elapsed = finish.to\_f - start.to\_f # note to\_f to keep fractional  
> > \>\> seconds  
> > \>  
> > \> Yep, I assumed he wanted to stop at the seconds level.  
> > \>  
> > \>\> =\> 289.695813894272  
> > \>\> \> mins, secs = elapsed.divmod 60.0  
> > \>\> =\> [4, 49.6958138942719]  
> > \>\> \> puts("%3d:%04.2f"%[mins.to\_i, secs])  
> > how this line work? "%3d" and %04.2f" means selecting min and secs  
> > respectively?
> > 
> > \>\> 4:49.70  
> > \>\> =\> nil  
> > \>  
> > \> I always forget about divmod, it's a cool little method 🙂  
> > \>  
> > \> Jesus.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Clement\_Ow](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clement\_Ow](https://rubytalk.org/u/Clement_Ow)\
**Post date:** [17 April 2008 02:33 UTC](https://rubytalk.org/t/calculating-elapsed-time/45916/8 "2008-04-17T02:33:34Z")

</div>

That's some information, thanks, dan!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
