DevBase.Extensions
1.0.2
dotnet add package DevBase.Extensions --version 1.0.2
NuGet\Install-Package DevBase.Extensions -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DevBase.Extensions" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DevBase.Extensions" Version="1.0.2" />
<PackageReference Include="DevBase.Extensions" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DevBase.Extensions --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DevBase.Extensions, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DevBase.Extensions@1.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DevBase.Extensions&version=1.0.2
#tool nuget:?package=DevBase.Extensions&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DevBase.Extensions
DevBase.Extensions provides extension methods and utilities for .NET 9.0, focusing on enhanced Stopwatch functionality with detailed time breakdowns and formatted output.
Features
- Stopwatch Extensions - Enhanced timing with detailed breakdowns
- Time Utilities - Time conversion and formatting helpers
- Console Table Output - Pretty-printed time tables in Markdown format
Installation
dotnet add package DevBase.Extensions
Stopwatch Extensions
Enhanced stopwatch functionality with detailed time breakdowns.
Basic Usage
using DevBase.Extensions.Stopwatch;
using System.Diagnostics;
var stopwatch = Stopwatch.StartNew();
// ... perform operations ...
stopwatch.Stop();
// Print detailed time table
stopwatch.PrintTimeTable();
Output Example
| Data | Unit |
|------|--------------|
| 2 | Hours |
| 15 | Minutes |
| 30 | Seconds |
| 500 | Milliseconds |
| 1234 | Microseconds |
| 5678 | Nanoseconds |
Get Time Table as String
var stopwatch = Stopwatch.StartNew();
// ... operations ...
stopwatch.Stop();
string timeTable = stopwatch.GetTimeTable();
Console.WriteLine(timeTable);
// Or save to file
File.WriteAllText("timing.md", timeTable);
Time Utilities
Helper methods for time conversions and formatting.
Get Individual Time Components
using DevBase.Extensions.Utils;
using System.Diagnostics;
var stopwatch = Stopwatch.StartNew();
// ... operations ...
stopwatch.Stop();
// Get hours
(int hours, string unit) = TimeUtils.GetHours(stopwatch);
Console.WriteLine($"{hours} {unit}"); // "2 Hours"
// Get minutes
(int minutes, string unit) = TimeUtils.GetMinutes(stopwatch);
Console.WriteLine($"{minutes} {unit}"); // "15 Minutes"
// Get seconds
(int seconds, string unit) = TimeUtils.GetSeconds(stopwatch);
Console.WriteLine($"{seconds} {unit}"); // "30 Seconds"
// Get milliseconds
(int milliseconds, string unit) = TimeUtils.GetMilliseconds(stopwatch);
Console.WriteLine($"{milliseconds} {unit}"); // "500 Milliseconds"
// Get microseconds
(long microseconds, string unit) = TimeUtils.GetMicroseconds(stopwatch);
Console.WriteLine($"{microseconds} {unit}"); // "1234 Microseconds"
// Get nanoseconds
(long nanoseconds, string unit) = TimeUtils.GetNanoseconds(stopwatch);
Console.WriteLine($"{nanoseconds} {unit}"); // "5678 Nanoseconds"
Usage Examples
Benchmarking Operations
using DevBase.Extensions.Stopwatch;
using System.Diagnostics;
public void BenchmarkOperation()
{
var stopwatch = Stopwatch.StartNew();
// Perform operation
for (int i = 0; i < 1000000; i++)
{
_ = i * 2;
}
stopwatch.Stop();
Console.WriteLine("Operation completed:");
stopwatch.PrintTimeTable();
}
Comparing Multiple Operations
public void CompareOperations()
{
// Operation 1
var sw1 = Stopwatch.StartNew();
PerformOperation1();
sw1.Stop();
// Operation 2
var sw2 = Stopwatch.StartNew();
PerformOperation2();
sw2.Stop();
Console.WriteLine("Operation 1:");
Console.WriteLine(sw1.GetTimeTable());
Console.WriteLine("\nOperation 2:");
Console.WriteLine(sw2.GetTimeTable());
}
Logging Performance Metrics
using DevBase.Extensions.Stopwatch;
using DevBase.Logging.Logger;
public async Task<T> MeasureAsync<T>(Func<Task<T>> operation, string operationName)
{
var stopwatch = Stopwatch.StartNew();
try
{
var result = await operation();
stopwatch.Stop();
var logger = new Logger<string>("Performance");
logger.Write($"{operationName} completed:\n{stopwatch.GetTimeTable()}", LogType.INFO);
return result;
}
catch (Exception ex)
{
stopwatch.Stop();
Console.WriteLine($"{operationName} failed after {stopwatch.Elapsed}");
throw;
}
}
API Response Time Tracking
using DevBase.Net.Core;
using DevBase.Extensions.Stopwatch;
public async Task<Response> TimedRequest(string url)
{
var stopwatch = Stopwatch.StartNew();
var response = await new Request(url).SendAsync();
stopwatch.Stop();
Console.WriteLine($"Request to {url}:");
stopwatch.PrintTimeTable();
return response;
}
Database Query Performance
public async Task<List<User>> GetUsersWithTiming()
{
var stopwatch = Stopwatch.StartNew();
var users = await database.Users.ToListAsync();
stopwatch.Stop();
Console.WriteLine($"Retrieved {users.Count} users:");
stopwatch.PrintTimeTable();
return users;
}
File Processing Timing
using DevBase.IO;
using DevBase.Extensions.Stopwatch;
public void ProcessFilesWithTiming(string directory)
{
var stopwatch = Stopwatch.StartNew();
var files = AFile.GetFiles(directory, readContent: true);
foreach (var file in files)
{
ProcessFile(file);
}
stopwatch.Stop();
Console.WriteLine($"Processed {files.Length} files:");
stopwatch.PrintTimeTable();
}
Advanced Usage
Custom Time Formatting
public string FormatElapsedTime(Stopwatch stopwatch)
{
var (hours, _) = TimeUtils.GetHours(stopwatch);
var (minutes, _) = TimeUtils.GetMinutes(stopwatch);
var (seconds, _) = TimeUtils.GetSeconds(stopwatch);
var (milliseconds, _) = TimeUtils.GetMilliseconds(stopwatch);
if (hours > 0)
return $"{hours}h {minutes}m {seconds}s";
else if (minutes > 0)
return $"{minutes}m {seconds}s";
else if (seconds > 0)
return $"{seconds}s {milliseconds}ms";
else
return $"{milliseconds}ms";
}
Performance Threshold Alerts
public void CheckPerformance(Stopwatch stopwatch, TimeSpan threshold)
{
stopwatch.Stop();
if (stopwatch.Elapsed > threshold)
{
Console.WriteLine("⚠️ Performance threshold exceeded!");
stopwatch.PrintTimeTable();
}
}
Aggregate Timing Statistics
public class TimingStats
{
private List<TimeSpan> timings = new();
public void AddTiming(Stopwatch stopwatch)
{
timings.Add(stopwatch.Elapsed);
}
public void PrintStatistics()
{
var avg = TimeSpan.FromTicks((long)timings.Average(t => t.Ticks));
var min = timings.Min();
var max = timings.Max();
Console.WriteLine($"Average: {avg}");
Console.WriteLine($"Min: {min}");
Console.WriteLine($"Max: {max}");
}
}
Exception Handling
StopwatchException
Thrown when trying to get time table from a running stopwatch:
using DevBase.Extensions.Exceptions;
try
{
var stopwatch = Stopwatch.StartNew();
// Don't stop it
stopwatch.PrintTimeTable(); // Throws StopwatchException
}
catch (StopwatchException ex)
{
Console.WriteLine("Stopwatch must be stopped first");
stopwatch.Stop();
stopwatch.PrintTimeTable();
}
Integration with Other DevBase Libraries
With DevBase.Net
using DevBase.Net.Core;
using DevBase.Extensions.Stopwatch;
var stopwatch = Stopwatch.StartNew();
var response = await new Request("https://api.example.com").SendAsync();
stopwatch.Stop();
Console.WriteLine("API Request:");
stopwatch.PrintTimeTable();
Console.WriteLine($"Response: {response.StatusCode}");
With DevBase.Api
using DevBase.Api.Apis.Deezer;
using DevBase.Extensions.Stopwatch;
var stopwatch = Stopwatch.StartNew();
var deezer = new Deezer();
var results = await deezer.Search("Rick Astley");
stopwatch.Stop();
Console.WriteLine("Deezer Search:");
stopwatch.PrintTimeTable();
With DevBase.Format
using DevBase.Format;
using DevBase.Format.Formats.LrcFormat;
using DevBase.Extensions.Stopwatch;
var stopwatch = Stopwatch.StartNew();
var parser = new FileParser<LrcFormat, AList<TimeStampedLyric>>();
var lyrics = parser.ParseFromDisk("song.lrc");
stopwatch.Stop();
Console.WriteLine("LRC Parsing:");
stopwatch.PrintTimeTable();
Performance Tips
- Stop before printing - Always stop stopwatch before calling
GetTimeTable()orPrintTimeTable() - Reuse stopwatch instances - Use
Restart()instead of creating new instances - Minimal overhead - Extension methods have negligible performance impact
- High precision - Uses
Stopwatch.Frequencyfor accurate measurements
Common Patterns
Pattern 1: Try-Finally Timing
var stopwatch = Stopwatch.StartNew();
try
{
PerformOperation();
}
finally
{
stopwatch.Stop();
stopwatch.PrintTimeTable();
}
Pattern 2: Async Operation Timing
var stopwatch = Stopwatch.StartNew();
await PerformAsyncOperation();
stopwatch.Stop();
stopwatch.PrintTimeTable();
Pattern 3: Conditional Timing
Stopwatch stopwatch = null;
if (enableTiming)
{
stopwatch = Stopwatch.StartNew();
}
PerformOperation();
if (stopwatch != null)
{
stopwatch.Stop();
stopwatch.PrintTimeTable();
}
Target Framework
- .NET 9.0
Dependencies
- ConsoleTables - For formatted table output
- System.Diagnostics - Stopwatch functionality
License
MIT License - See LICENSE file for details
Author
AlexanderDotH
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- ConsoleTables (>= 2.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.