Solved

Measure time (ms) to complete task

Posted on 2003-12-12
5
890 Views
Last Modified: 2013-11-15
Hi, I have console program that I'll try to optimizie for speed. I need a way to measure how long it takes to complete certain tasks. I'd like to be able to say something like this anywhere in the code:

// Start counting from here

// Do something...

// Report time in milliseconds here

Ideas? I've been looking at the Timer class, but since these are just temporary checks, I'd rather not put everything in a while block. I hope there's a way to just get the time at two points and subtract the first from the second.
0
Comment
Question by:liljegren
5 Comments
 

Expert Comment

by:windsofchange
ID: 9927620
long begtime=System.DateTime.Now.Ticks;

// do something

long endtime=System.DateTime.Now.Ticks;

long ergtime=endtime-begtime;
double ergsec=Math.Round((double)ergtime/10000000D,2);

gives you the time in seconds with 2 commas
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 9927819
DateTime start = DateTime.Now;

// do something

TimeSpan timeElapsed = DateTime.Now - start;

timeElapsed.ToString() -> gives you the time in the format [-][d.]hh:mm:ss[.ff]
timeElapsed.Seconds, Milliseconds, Minutes, Days, Hours, Ticks, etc. can give you all the info you may need.

For more info see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtimespanclasstopic.asp
0
 
LVL 18

Expert Comment

by:testn
ID: 9927980
You'd better use Hi Performance Timer since the Datetime object can measure up to 10ms accuracy.
http://www.codeproject.com/csharp/highperformancetimercshar.asp
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9928039
testn, there is always something to learn from you! 10x although your answer was not accepted!
0
 

Author Comment

by:liljegren
ID: 9928062
Yes, thanks for the link testn. For now, the answer from TheAvenger is good enough for me, but I'll check it later.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question