Forum: Ruby DateTime.parse speed question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-13 19:15
(Received via mailing list)
I'm trying to speed up a small app that I've written.  When I run the
profiler I see that 46% of my applications time is spent on Integer#gcd.
This seems to be coming from DateTime.  I need to store dates, and then
find the differences in time between these dates.  Is there anything I
can do differently or do I just have to suffer the consequences of using
ruby's DateTime class?


example

diff = DateTime.now - data_from_file
h,m,s,frac = DateTime.day_fraction_to_time(diff)

another example

todays_data << [DateTime.parse(time),status,task.chomp]
E5c79d5a6d378dc4a26e20435be31657?d=identicon&s=25 Jamey Cribbs (Guest)
on 2006-02-13 19:30
(Received via mailing list)
Charlie Bowman wrote:

>diff = DateTime.now - data_from_file
>h,m,s,frac = DateTime.day_fraction_to_time(diff)
>
>another example
>
>todays_data << [DateTime.parse(time),status,task.chomp]
>
>
DateTime, being written in Ruby itself, is pretty slow.  If you can use
the Time class instead, you will see a significant performance boost,
since it is written in C.

There is one gotcha about the Time class:  on Windows, you can't create
a Time object earlier than January 1, 1970 (this is not a limitation on
*nix).  If this is not an issue for you, you might be able to get away
with using Time.

Jamey
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-13 20:10
(Received via mailing list)
The Time class doesn't appear to have any methods to parse a string into
a time object or to find the difference of two times.  How can this be
done without using the DateTime class?
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2006-02-13 20:24
Charlie Bowman wrote:
> The Time class doesn't appear to have any methods to parse a string into
> a time object or to find the difference of two times.  How can this be
> done without using the DateTime class?

ri Time.parse

freebie:~ 701> irb -r time
irb(main):001:0> Time.parse( "Mon Feb 13 14:22:10 EST 2006" )
=> Mon Feb 13 14:22:10 EST 2006
irb(main):002:0> t = Time.parse( "Mon Feb 13 14:22:10 EST 2006" )
=> Mon Feb 13 14:22:10 EST 2006
irb(main):003:0> t = t + 50
=> Mon Feb 13 14:23:00 EST 2006
irb(main):004:0> t = t + 3600
=> Mon Feb 13 15:23:00 EST 2006
irb(main):005:0> t_hour_later = t + 3600
=> Mon Feb 13 16:23:00 EST 2006
irb(main):006:0> t_hour_later - t
=> 3600.0
E5c79d5a6d378dc4a26e20435be31657?d=identicon&s=25 Jamey Cribbs (Guest)
on 2006-02-13 20:35
(Received via mailing list)
Charlie Bowman wrote:

>The Time class doesn't appear to have any methods to parse a string into
>a time object or to find the difference of two times.  How can this be
>done without using the DateTime class?
>
>
As Mike Fletcher answered in another email, there is a Standard Library
module called Time, that you can require into your script.  This adds a
parse method to the Time built-in class.

If you don't want to do that, you could roll your own by using
Time.local and parsing the string yourself and feeding the results to
Time.local.

As far as finding the difference between two times, you can use the "-"
instance method to find the difference in seconds.  If you need that in
hours or minutes, just have your script do the math.

Definitely more work, but if you are looking for more speed than
DateTime, it might be worth it.

Jamey
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-13 20:44
(Received via mailing list)
Thanks!
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-21 16:45
(Received via mailing list)
I decided to figure out just how much faster using Time was than using
DateTime so I wrote a couple of benchmark scripts.  It turns out that
using Time offered an 800% increase in speed in my test app.  I knew
that Time was faster but I never thought it would be that much of a
difference.  I posted both scripts and a longer explanation at
recentrambles.com.  I would love to know if there are better ways to
work with dates than what I used.  I'm still trying to get the perl out
of my ruby!
This topic is locked and can not be replied to.