[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: DateTime.parse speed question

Jamey Cribbs

2/13/2006 6:31:00 PM

Charlie Bowman wrote:

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


4 Answers

Charlie Bowman

2/13/2006 7:10:00 PM

0

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?


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

Jamey Cribbs

2/13/2006 7:35:00 PM

0

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


Charlie Bowman

2/13/2006 7:44:00 PM

0

Thanks!

On Tue, 2006-02-14 at 04:34 +0900, Jamey Cribbs wrote:

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

Charlie Bowman

2/21/2006 3:43:00 PM

0

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!


On Tue, 2006-02-14 at 04:34 +0900, Jamey Cribbs wrote:

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