473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

How do I calculate elapsed time in milliseconds?

I want to determine the elapsed time between two events. What is the best
way to do this?
Nov 16 '05 #1
5 68868
JSmith,

The best way would be to get the value returned by the static Now
property on the DateTime class when the first event fires. When the second
event fires, get the time returned from the Now property again. Then, you
can take the difference between the two.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JSmith" <nf*@nospam.com> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
I want to determine the elapsed time between two events. What is the best
way to do this?

Nov 16 '05 #2
try this:
DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;
// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Milliseconds ;
Vladimir

"JSmith" <nf*@nospam.com> wrote in message news:eV**************@tk2msftngp13.phx.gbl...
I want to determine the elapsed time between two events. What is the best
way to do this?

Nov 16 '05 #3
Yes, this is true, but keep in mind that its not an instantanious
operation. i.e its based CPU load and the timeslice that the current
thread gets, that the Now operation is dependent on. It will probably be
off by only millis but off nevertheless.

Nicholas Paldino [.NET/C# MVP] wrote:
JSmith,

The best way would be to get the value returned by the static Now
property on the DateTime class when the first event fires. When the second
event fires, get the time returned from the Now property again. Then, you
can take the difference between the two.

Hope this helps.


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #4
Why not just use the Environment.TickCount property. Get the value when the
first and second event fires, then subtract the first value from the second.
That'll give you the difference in milliseconds.
"JSmith" <nf*@nospam.com> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
I want to determine the elapsed time between two events. What is the best
way to do this?

Nov 16 '05 #5
Not sure if this does what you want, but I like using unix time stamp in
general:

double my_start_time, my_end_time, my_diff;
my_start_time = (DateTime.UtcNow - new
DateTime(1970,1,1,0,0,0)).TotalSeconds;

// for loop or some events goes in here

my_end_time = (DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;

my_diff = my_end_time-my_start_time;


"JSmith" <nf*@nospam.com> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
I want to determine the elapsed time between two events. What is the best
way to do this?

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
6
by: Thomas Baier | last post by:
Hi there, I need to know how to get the elapsed time in milliseconds. I tried time_t start,end; time(start); do_something(); time(end); double dif = difftime(end, start);
4
by: JSmith | last post by:
I want to determine the elapsed time between two events. What is the best way to do this?
5
by: techspirit | last post by:
Hi, I am seeking help for calculating the elapsed time on an activity. When the user clicks "pause" button on a form, the timer should also pause. When the user clicks "continue", the timer...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.