473,396 Members | 2,109 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,396 software developers and data experts.

Counting the execution time of a Task

Hello,
I am checking out this simple piece of code:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             Stopwatch sw = Stopwatch.StartNew();
  4.  
  5.             Task Task1 = Task.Factory.StartNew(() =>
  6.                 {
  7.                     Console.WriteLine("Task1 started in : "+ " "+sw.ElapsedMilliseconds);
  8.                     Thread.Sleep(4000);
  9.                     Console.WriteLine("Task1 finished in : " + " " + sw.ElapsedMilliseconds);
  10.                 }
  11.                 );
  12.         }
and the results are :
Task1 started in : 2 in milliseconds
Task1 finished in : 4015 in milliseconds.
The problem is, that if i count the time with an external device, just like the timer of my watch or my cellphone s,
the total time is 4,8 or 4,9 seconds, which i wouldn t expect that to happen.
The first thing i ve thought, is that it takes the ThreadPool about 800msec or 1 sec to create the thread so as
to execute this task, but even if it is so, shouldn t the stopwatch count the time precisely?
So , why is there a deviation between an external timer and the stopwatch ?
Thanx in advance.
Nov 9 '13 #1
1 4312
Rabbit
12,516 Expert Mod 8TB
Because your human hands sttempting to precisely time the execution is slower and less accurate than the computer. The results are correct.
Nov 12 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
5
by: Johannes Lebek | last post by:
Hi there, lately, I experienced a strange thing on my DB2 V8.1 on Windows: Some queries took a very long time. A snapshot discovered the following: Number of executions = 47...
1
by: UnixSlaxer | last post by:
Hello all, I installed the trial version of DB2 UDB 8.2, I was wondering, how can I determine the exact execution time (in miliseconds not timerons) for a specific query ? Thank you very...
7
by: Tim Quon | last post by:
Hi Is there any function to get the current time so I can calculate the execution time of my code? What all is in the time.h and sys/times.h? Thanks Tim
27
by: vishnu mahendra | last post by:
cah you please tell me how to find the execution time of a program in c. thank you in advance, vishnu
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
3
by: iam980 | last post by:
Hello All. We have tested following SQL script from query analyzer: -- Script begin DECLARE @I int; SET @I = 1; WHILE @I < 10000000 BEGIN SET @I = @I + 1; END -- Script end
3
by: vashwath | last post by:
Hi all, I have written program for calculating the execution time of a function.Any critics (on the method of calculating execution time) are welcome. #include <stdio.h> #include <time.h>...
6
by: monkeyboy | last post by:
Hello, I have a function that hotshot says is very slow. I can get the aggregate execution time, but is there a way to get the execution time of each line so I can find the bottleneck? Thank...
6
by: Mikhail Kovalev | last post by:
I'm using set_time_limit() to set maximum execution time. Is there a way to check how much time is left at any time during the execution itself?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.