Hi
Im trying to profile some code in a backgroundworker (wpf application, .net 4.0 Client Profile). The pseudocode is:
void bw_DoWork(object sender, DoWorkEventArgs e)
{
    var stopWatch = Stopwatch.StartNew();
    
     // slow running code
    ...

    stopWatch.Stop();
    Debug.WriteLine("Time taken: "+stopWatch.ElapsedMilliseconds+" ms");    
}

My stopWatch prints out: "Time taken: 119823 ms", but the profiler displays the following values for "Time With Children" (bw_DoWork)

CPU time + Miliseconds : 26.524,787
Wallclock time + miliseconds: 48.699,648

Do you know why I get such a big difference? I could understand if profiled time was greater than the stopwatch due to overhead when creating the thread ect, but how come it is less?
BrcSen
0

Comments

3 comments

  • Chris.Allen
    Was this in sampling mode? Sometimes, when sampling mode doesn't have enough data points it can result in an underestimate.
    Chris.Allen
    0
  • BrcSen
    I just tried to select Async Mode under View before pressing executing the code I was trying to profile. This gives the result:

    Stopwatch: 458056ms
    Wall Time: 442265ms
    CPU Time: 372429ms

    The wall-clock time is much more accurate now, thanks :)
    When should I use async vs sampling?
    BrcSen
    0
  • Chris.Allen
    Sampling is the mode of choice for longer running projects or projects with a very high degree of CPU usage. The Async feature is really designed to work with the new async keyword. It's great news that you're getting better data now :)
    Chris.Allen
    0

Add comment

Please sign in to leave a comment.