0Pricing
C# Academy · Lesson

Basic profiling & traces (concepts)

Measure code with Stopwatch, create tiny timing scopes, understand trace correlation IDs, and learn basic micro-benchmark hygiene.

Basic profiling & traces (concepts) is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Timing & tracing basics

Aim:

  • Measure hot paths with Stopwatch
  • Add tiny timing scopes for blocks
  • Use trace correlation IDs to link events
  • Avoid noisy measurements

Stopwatch demo

Use Stopwatch to time code; warm up first and repeat the work to smooth out JIT noise.

using System;
using System.Diagnostics;
using System.Threading;

// Simple loop timing with warmup; C# 6 compatible.
public class Program
{
  static int Work(int n)
  {
    int s = 0;
    for (int i = 0; i < n; i++) s += i; // small CPU work
    return s;
  }

  public static void Main(string[] args)
  {
    // Warm up (JIT/caches)
    Work(10000);

    var sw = Stopwatch.StartNew();
    for (int r = 0; r < 10; r++)
    {
      Work(20000);
    }
    sw.Stop();

    Console.WriteLine("Elapsed ms (10 runs): " + sw.ElapsedMilliseconds);
  }
}

Timing scope helper

Wrap blocks in a TimeScope to see duration for named sections without repeating Stopwatch code.

using System;
using System.Diagnostics;

// A small scope that times a block and prints on dispose.
public sealed class TimeScope : IDisposable
{
  private readonly string _name;
  private readonly Stopwatch _sw;

  public TimeScope(string name)
  {
    _name = name;
    _sw = Stopwatch.StartNew();
  }

  public void Dispose()
  {
    _sw.Stop();
    Console.WriteLine(_name + " took " + _sw.ElapsedMilliseconds + " ms");
  }
}

public class Program
{
  static void DoWork()
  {
    // pretend work
    long s = 0;
    for (int i = 0; i < 300000; i++) s += i;
    if (s == -1) Console.WriteLine();
  }

  public static void Main(string[] args)
  {
    using (new TimeScope("DoWork"))
    {
      DoWork();
    }
  }
}

Correlation ID demo

Set a correlation ID (ActivityId) to link related trace messages across calls.

using System;
using System.Diagnostics;

// Use a correlation ID so related trace lines can be grouped.
public class Program
{
  public static void Main(string[] args)
  {
    Trace.Listeners.Clear();
    Trace.Listeners.Add(new ConsoleTraceListener());

    Guid id = Guid.NewGuid();
    Trace.CorrelationManager.ActivityId = id;

    Trace.TraceInformation("Start request");
    // ... do work
    Trace.TraceInformation("Processing step A");
    Trace.TraceInformation("Processing step B");
    Trace.TraceInformation("End request");

    Console.WriteLine("ActivityId: " + id);
  }
}

Profiling overview

  • Sampling profilers: capture stack snapshots periodically (low overhead)
  • Instrumentation: measure specific blocks (Stopwatch/timing scopes)
  • Start broad with sampling; confirm with targeted timing

Measurement hygiene

Tips:

  • Measure in Release with optimizations
  • Avoid Console.WriteLine inside timed code
  • Repeat runs; report averages
  • Time realistic input sizes

Micro-benchmark rule

Quick check: What is a good basic practice when micro-benchmarking code with Stopwatch?

Recap

Recap: Use Stopwatch and tiny timing scopes to time blocks; add a correlation ID to link traces; measure cleanly (warmup, repeat, Release).

Frequently asked questions

Is the “Basic profiling & traces (concepts)” lesson free?

Yes — the full text of “Basic profiling & traces (concepts)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Basic profiling & traces (concepts)”?

Measure code with Stopwatch, create tiny timing scopes, understand trace correlation IDs, and learn basic micro-benchmark hygiene. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Basic profiling & traces (concepts)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Logging abstractions, Debug/Trace
  2. Basic profiling & traces (concepts)
  3. Guard & validation patterns
← Back to C# Academy