473,385 Members | 2,044 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,385 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 68861
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.