Skip to content

shamuddin/TimeMeasurementInDotNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

⏳ Execution Time Measurement In DotNet

A project to demonstrate the correct way to measure execution time in .NET. 🛠️ Learn how to avoid common pitfalls, optimize performance, and measure time like a pro!


📖 Overview

This repository contains examples of three methods to measure execution time in .NET:

  1. DateTime.UtcNow: A commonly used but inefficient approach.
  2. Stopwatch.StartNew: Better, but introduces unnecessary overhead.
  3. Stopwatch.GetTimestamp: The most accurate and efficient method.

Each method is benchmarked using BenchmarkDotNet to provide proof of performance. 💡


⚙️ Technologies Used

  • .NET 8.0
  • C#
  • BenchmarkDotNet

📂 Project Structure

TimeMeasurementInDotNet/
├── TimeMeasurementInDotNet/      # Main project folder
│   ├── Program.cs                # Entry point for running benchmarks
│   ├── TimeMeasurementBenchmarks.cs  # Contains benchmark tests for all three methods
├── .gitignore                    # Ignore unnecessary files in Git
├── LICENSE                       # License information
├── README.md                     # Project documentation

🚀 How to Run

Follow these steps to run the benchmarks and view the results:

  1. Clone the Repository

    git clone https://github.com/<your-username>/TimeMeasurementInDotNet.git
    cd TimeMeasurementInDotNet
  2. Restore Dependencies

    dotnet restore
  3. Run Benchmarks
    Make sure you are in Release mode before running:

    dotnet run --configuration Release
  4. View Results
    Results will appear in the console and as .html and .md files under:

    bin/Release/net8.0/BenchmarkDotNet.Artifacts/results/
    

🔬 Benchmark Results

🛠️ Method 🕒 Mean 📉 Error 📊 StdDev 💾 Allocated
MeasureWithDateTime 509.0 ms 1.77 ms 1.66 ms 1.1 KB
MeasureWithStopwatchStartNew 509.6 ms 1.34 ms 1.25 ms 1.14 KB
MeasureWithStopwatchGetTimestamp 509.3 ms 1.42 ms 1.32 ms 1.1 KB

🔍 Insights:

  • DateTime.UtcNow: Adds unnecessary overhead due to internal date calculations.
  • Stopwatch.StartNew: Allocates memory unnecessarily.
  • Stopwatch.GetTimestamp: Fast, precise, and memory-efficient.

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.


📚 References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages