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]
Charlie B. 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
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?
Charlie B. 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
Charlie B. 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 F. 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
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!