473,326 Members | 2,013 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,326 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 68840
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.