[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework

Performance hit because of Datetime.Now

chivateatul

10/1/2008 4:55:00 PM

If DateTime.Now is called in a very large loop, it's a time consuming
& also CPU utilization increases.
Instead, getting current datetime by following way is much more
efficient.

So is there any performance issue with DateTime.Now? is this a correct
way to get current datetime?

Pseudo Code:
- - - - - - - - - - -
static DateTime dateTime = DateTime.Now;
static int StartTicks = Environment.TickCount;

public DateTime CurrentDateTime
{
get
{
return dateTime.AddMilliseconds(Environment.TickCount -
StartTicks);
}
}
25 Answers

Brian Gideon

10/1/2008 5:10:00 PM

0

On Oct 1, 11:55 am, chivatea...@gmail.com wrote:
> If DateTime.Now is called in a very large loop, it's a time consuming
> & also CPU utilization increases.
> Instead, getting current datetime by following way is much more
> efficient.
>
> So is there any performance issue with DateTime.Now? is this a correct
> way to get current datetime?
>
> Pseudo Code:
> - - - - - - - - - - -
> static DateTime dateTime = DateTime.Now;
> static int StartTicks = Environment.TickCount;
>
> public DateTime CurrentDateTime
> {
>    get
>    {
>        return dateTime.AddMilliseconds(Environment.TickCount -
> StartTicks);
>    }
>
>
>
> }

DateTime.Now actually calls DateTime.UtcNow and then converts the
result to the local time zone. I did a quick test and observed that
UtcNow is about 50x faster.

chivateatul

10/1/2008 5:34:00 PM

0

On Oct 1, 1:10 pm, Brian Gideon <briangid...@yahoo.com> wrote:
> On Oct 1, 11:55 am, chivatea...@gmail.com wrote:
>
>
>
>
>
> > If DateTime.Now is called in a very large loop, it's a time consuming
> > & also CPU utilization increases.
> > Instead, getting current datetime by following way is much more
> > efficient.
>
> > So is there any performance issue with DateTime.Now? is this a correct
> > way to get current datetime?
>
> > Pseudo Code:
> > - - - - - - - - - - -
> > static DateTime dateTime = DateTime.Now;
> > static int StartTicks = Environment.TickCount;
>
> > public DateTime CurrentDateTime
> > {
> >    get
> >    {
> >        return dateTime.AddMilliseconds(Environment.TickCount -
> > StartTicks);
> >    }
>
> > }
>
> DateTime.Now actually calls DateTime.UtcNow and then converts the
> result to the local time zone.  I did a quick test and observed that
> UtcNow is about 50x faster.- Hide quoted text -
>
> - Show quoted text -


Thanx Brian,

But from Reflector, DateTime.Now calls GetSystemFileTime() & with
Datetime.UtcNow calls 'Win32Native.GetSystemTimeAsFileTime(ref
lpSystemTimeAsFileTime)'

So to get current datetime in a efficiently, which is the best way?


Peter Duniho

10/1/2008 5:39:00 PM

0

On Wed, 01 Oct 2008 09:55:06 -0700, <chivateatul@gmail.com> wrote:

> If DateTime.Now is called in a very large loop, it's a time consuming
> & also CPU utilization increases.

If you are calling DateTime.Now so often that it's a performance issue,
then you probably should fix the underlying design so that you don't call
it so often.

It's hard to know for sure what the best approach would be without knowing
more about why you're calling it. But it just doesn't seem to me that a
call to DateTime.Now should represent a significant part of your
processing, even as slow as it is.

Pete

chivateatul

10/1/2008 6:24:00 PM

0

On Oct 1, 1:39 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Wed, 01 Oct 2008 09:55:06 -0700, <chivatea...@gmail.com> wrote:
> > If DateTime.Now is called in a very large loop, it's a time consuming
> > & also CPU utilization increases.
>
> If you are calling DateTime.Now so often that it's a performance issue,  
> then you probably should fix the underlying design so that you don't call  
> it so often.
>
> It's hard to know for sure what the best approach would be without knowing  
> more about why you're calling it.  But it just doesn't seem to me that a  
> call to DateTime.Now should represent a significant part of your  
> processing, even as slow as it is.
>
> Pete

Thanx Pete,

Actually I am writing a gui in winform, which handles live data, &
that data is displayed in a grid(3rd party grid), in which when any
cell has to paint it raises event for each cell & in that event
depending on our business logic, we need to take datetime.now & the
previous datetime.Now, to know the timedifference between earlier
event call & current event call for that cell & since live data
updates are at a high rate this event gets fired continuously. Overall
gui is in a good shape to handle that data, but we are further
optimizing it at all levels, and there we find out problem with
DateTime.Now.

Actually my question is, has anyone idea that there is a problem with
DateTime.Now? Is it documented anywhere(MSDN???)

Peter Duniho

10/1/2008 7:04:00 PM

0

On Wed, 01 Oct 2008 11:23:50 -0700, <chivateatul@gmail.com> wrote:

> Actually I am writing a gui in winform, which handles live data, &
> that data is displayed in a grid(3rd party grid), in which when any
> cell has to paint it raises event for each cell & in that event
> depending on our business logic, we need to take datetime.now & the
> previous datetime.Now, to know the timedifference between earlier
> event call & current event call for that cell & since live data
> updates are at a high rate this event gets fired continuously.

Whatever the cost of retrieving DateTime.Now, it should pale in comparison
to the cost of actually updating the GUI. It seems to me that if your
data rate is so high that you're running into a performance issue with
DateTime.Now, then one important fix you probably should make is to
throttle the GUI update.

The user can only keep track of so much information at once anyway, and by
throttling the GUI update, you not only eliminate whatever marginal
influence the call to DateTime.Now might have, you also help ensure that
the application and overal computer remains more responsive, since you
don't wind up spending all your time trying to keep the UI up-to-date.

Keeping track of invalidated displayed data and updating the UI once a
second, or even once every half second, would surely not in any way
diminish the user experience, but would significantly improve any
performance issue you're having with respect to redrawing the UI.

Note also that if all you care about is elapsed time, you may find that a
better class to use is the Stopwatch class. This would be with or without
improvements as described above.

> Overall
> gui is in a good shape to handle that data, but we are further
> optimizing it at all levels, and there we find out problem with
> DateTime.Now.

I still find it difficult to understand how DateTime.Now could be causing
a bottleneck with respect to GUI updating. Perhaps if you could post a
concise-but-complete code sample that demonstrated the problem, we could
see why it is you are able to update the UI so quickly that the relatively
tiny cost of calling DateTime.Now (even in the inefficient way) is
dominating your performance data.

But in any case, it seems likely to me that at the very least, you can
dramatically improve your performance by not issuing a redraw every single
time some data shows up. Just update periodically, at a slower rate that
is more in line with what a human user is capable of perceiving.

> Actually my question is, has anyone idea that there is a problem with
> DateTime.Now? Is it documented anywhere(MSDN???)

Even with Brian's observation that DateTime.Now takes 50x longer than
DateTime.UtcNow, I remain unconvinced that there's "a problem with
DateTime.Now". It may be too slow for what you're trying to do with it,
but all that means is that you're not using it in an appropriate way.

Pete

Brian Gideon

10/1/2008 7:39:00 PM

0

On Oct 1, 12:33 pm, chivatea...@gmail.com wrote:
> On Oct 1, 1:10 pm, Brian Gideon <briangid...@yahoo.com> wrote:
>
>
>
>
>
> > On Oct 1, 11:55 am, chivatea...@gmail.com wrote:
>
> > > If DateTime.Now is called in a very large loop, it's a time consuming
> > > & also CPU utilization increases.
> > > Instead, getting current datetime by following way is much more
> > > efficient.
>
> > > So is there any performance issue with DateTime.Now? is this a correct
> > > way to get current datetime?
>
> > > Pseudo Code:
> > > - - - - - - - - - - -
> > > static DateTime dateTime = DateTime.Now;
> > > static int StartTicks = Environment.TickCount;
>
> > > public DateTime CurrentDateTime
> > > {
> > >    get
> > >    {
> > >        return dateTime.AddMilliseconds(Environment.TickCount -
> > > StartTicks);
> > >    }
>
> > > }
>
> > DateTime.Now actually calls DateTime.UtcNow and then converts the
> > result to the local time zone.  I did a quick test and observed that
> > UtcNow is about 50x faster.- Hide quoted text -
>
> > - Show quoted text -
>
> Thanx Brian,
>
> But from Reflector, DateTime.Now calls GetSystemFileTime() & with
> Datetime.UtcNow calls 'Win32Native.GetSystemTimeAsFileTime(ref
> lpSystemTimeAsFileTime)'
>
> So to get current datetime in a efficiently, which is the best way?

When I use Reflector I'm seeing that DateTime.Now calls UtcNow and
ToLocalTime. Based on the test I did UtcNow is significantly faster
probably because it doesn't have the extra step of calling
ToLocalTime. Getting in the habit of doing all time processing in UTC
is a good practice anyway. Convert to the local time zone only when
displaying the time to a user.

Brian Gideon

10/1/2008 7:48:00 PM

0

On Oct 1, 1:23 pm, chivatea...@gmail.com wrote:
> Actually I am writing a gui in winform, which handles live data, &
> that data is displayed in a grid(3rd party grid), in which when any
> cell has to paint it raises event for each cell & in that event
> depending on our business logic, we need to take datetime.now & the
> previous datetime.Now, to know the timedifference between earlier
> event call & current event call for that cell & since live data
> updates are at a high rate this event gets fired continuously. Overall
> gui is in a good shape to handle that data, but we are further
> optimizing it at all levels, and there we find out problem with
> DateTime.Now.
>

Like Peter I'm having hard believe that DateTime.Now is the dominating
factor with the performance issue you're seeing. Unless of course you
call it repeatedly in a tight loop. If that's case then cache the
result of DateTime.Now in a local variable before the loop starts.

> Actually my question is, has anyone idea that there is a problem with
> DateTime.Now? Is it documented anywhere(MSDN???)

I don't think there is a problem. It's slow because ToLocalTime is
also slow. Just take a look at what it's doing via Reflector.

chivateatul

10/1/2008 8:04:00 PM

0

On Oct 1, 3:48 pm, Brian Gideon <briangid...@yahoo.com> wrote:
> On Oct 1, 1:23 pm, chivatea...@gmail.com wrote:
>
> > Actually I am writing a gui in winform, which handles live data, &
> > that data is displayed in a grid(3rd party grid), in which when any
> > cell has to paint it raises event for each cell & in that event
> > depending on our business logic, we need to take datetime.now & the
> > previous datetime.Now, to know the timedifference between earlier
> > event call & current event call for that cell & since live data
> > updates are at a high rate this event gets fired continuously. Overall
> > gui is in a good shape to handle that data, but we are further
> > optimizing it at all levels, and there we find out problem with
> > DateTime.Now.
>
> Like Peter I'm having hard believe that DateTime.Now is the dominating
> factor with the performance issue you're seeing.  Unless of course you
> call it repeatedly in a tight loop.  If that's case then cache the
> result of DateTime.Now in a local variable before the loop starts.
>
> > Actually my question is, has anyone idea that there is a problem with
> > DateTime.Now? Is it documented anywhere(MSDN???)
>
> I don't think there is a problem.  It's slow because ToLocalTime is
> also slow.  Just take a look at what it's doing via Reflector.


Thanx guys,

I got result. In my code instead of dateTime.Now, I started using
Datetime.UtcNow as suggested by Brian.

Datetime.Now is not a bottleneck, but because of nature of the
requirenment, its getting called multiple times, and we were not
expecting that datetime.Now will be slow or Datetime.UtcNow will be
further faster than datetime.Now.
We know Datetime.Now is not a bottleneck in the gui, or as I mentioned
our gui is handling all tghose updates very efficiently but for
further improvement, there are many small things and each contribute
little penny and now we have to clear all those little things :-), and
in that context datetime.Now is one thing :-)

And as I mentioned, We were not expecting slowness from Datetime.Now,
but compared to Datetime.UtcNow its slow (if tested in a tight loop),
so I was just curious if there is any technical stuff that says
Datetime.Now is little slower than XXX method because of ......
But any way, for my project I got solution. Thanx

Peter Duniho

10/2/2008 12:22:00 AM

0

On Wed, 01 Oct 2008 13:04:15 -0700, <chivateatul@gmail.com> wrote:

> [...]
> We know Datetime.Now is not a bottleneck in the gui, or as I mentioned
> our gui is handling all tghose updates very efficiently but for
> further improvement, there are many small things and each contribute
> little penny and now we have to clear all those little things :-), and
> in that context datetime.Now is one thing :-)

This is what's known in the business as a premature optimization. I won't
make any guesses or assessments about your own qualifications, but suffice
to say that among _most_ experienced programmers, it's widely known that
one should not waste time on premature optimizations.

I'm glad you got the results you wanted, but for future reference your
time is probably better-spent on other things.

Pete

commander Serk , military spokesperson for United ethnic Chinese Wa Army of Shan state of

10/7/2009 1:25:00 AM

0

Himmler asked the Jews in Poland
how much
they Jews were stealing from the Poles every 24 hours ?

Malays can also ask the Chinese in Malaysia how much
the Chinese are stealing from the Malay nation every 24
hours ?








On Oct 6, 2:56 pm, "Komin the almighy fucking failure analyst from
CUNT_bodia, fake Khmer aka fake wakalukong and monster"
<real_ko...@hotmail.com> wrote:
> What say fucker Komin, come back from a break, fuck here and there
> already fucker.