在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
在广泛的编程语言领域中,C# 作为一种多功能且强大的语言脱颖而出,广泛用于开发各类应用程序,从桌面应用到网页和移动应用。 C# 受到开发者喜爱的主要原因之一是其丰富的库和类,提供了解决各种编程难题的解决方案。 其中,Stopwatch 类因其在精确计时、分析和性能分析中的作用而占有特殊地位。
在本文中,我们将看到如何在C#中使用Stopwatch对象来通过公共TimeSpan Elapsed属性来找到执行特定任务所花费的时间。 另外,我们将记录使用IronPDF for C# Developers创建PDF的总耗时。
Stopwatch
类是 C# 中 System.Diagnostics
命名空间的一部分,提供了一种简单而高效的方法来以高精度测量经过的时间。 它是随着.NET Framework推出的,并且一直是开发人员跟踪代码段执行时间、优化性能和分析应用程序的宝贵工具。
使用Stopwatch
类非常简单。 要开始使用它,您首先需要创建一个新的Stopwatch
类实例:
using System.Diagnostics;
class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch(); // Stopwatch object
}
}
using System.Diagnostics;
class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch(); // Stopwatch object
}
}
Imports System.Diagnostics
Friend Class Program
Shared Sub Main()
Dim stopwatch As New Stopwatch() ' Stopwatch object
End Sub
End Class
一旦创建了Stopwatch
实例,您可以启动和停止秒表以测量经过的时间:
stopwatch.Start();
Console.WriteLine("It will measure the time between start and stop");
stopwatch.Stop(); // Stop measuring elapsed time
stopwatch.Start();
Console.WriteLine("It will measure the time between start and stop");
stopwatch.Stop(); // Stop measuring elapsed time
stopwatch.Start()
Console.WriteLine("It will measure the time between start and stop")
stopwatch.Stop() ' Stop measuring elapsed time
可以使用Elapsed
属性获取经过的时间:
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Elapsed time: {elapsed}");
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Elapsed time: {elapsed}");
Dim elapsed As TimeSpan = stopwatch.Elapsed
Console.WriteLine($"Elapsed time: {elapsed}")
输出:
Stopwatch
类除了基本的时间测量外,还提供了一些高级功能。 让我们来探索一些这些功能:
Restart
方法是一种方便的方式,可以在一次操作中停止并将经过的时间重置为零。 这在测量多个代码段的执行时间时非常有用,而无需创建新的Stopwatch
实例。
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine("The time will restart after executing the below code");
stopwatch.Restart();
stopwatch.Stop();
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Total Elapsed time after Restart: {elapsed}");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine("The time will restart after executing the below code");
stopwatch.Restart();
stopwatch.Stop();
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Total Elapsed time after Restart: {elapsed}");
Dim stopwatch As New Stopwatch()
stopwatch.Start()
Console.WriteLine("The time will restart after executing the below code")
stopwatch.Restart()
stopwatch.Stop()
Dim elapsed As TimeSpan = stopwatch.Elapsed
Console.WriteLine($"Total Elapsed time after Restart: {elapsed}")
输出:
IsHighResolution
属性IsHighResolution
属性指示底层计时机制是否基于高分辨率性能计数器来精确测量经过时间。当处理可能不支持高分辨率计时方法的系统时,检查此属性可能会很有用。
Stopwatch stopwatch = new Stopwatch();
if (Stopwatch.IsHighResolution)
{
Console.WriteLine("High-resolution timing is supported");
}
else
{
Console.WriteLine("Fallback to lower-resolution timing");
}
Stopwatch stopwatch = new Stopwatch();
if (Stopwatch.IsHighResolution)
{
Console.WriteLine("High-resolution timing is supported");
}
else
{
Console.WriteLine("Fallback to lower-resolution timing");
}
Dim stopwatch As New Stopwatch()
If System.Diagnostics.Stopwatch.IsHighResolution Then
Console.WriteLine("High-resolution timing is supported")
Else
Console.WriteLine("Fallback to lower-resolution timing")
End If
输出:
Frequency
属性返回底层计时器的频率,以每秒时钟周期数表示。 此值用于将经过的计时周期转换为其他时间单位,例如毫秒。
Stopwatch stopwatch = new Stopwatch();
long frequency = Stopwatch.Frequency;
Console.WriteLine($"Timer Frequency: {frequency} ticks per second");
Stopwatch stopwatch = new Stopwatch();
long frequency = Stopwatch.Frequency;
Console.WriteLine($"Timer Frequency: {frequency} ticks per second");
Dim stopwatch As New Stopwatch()
Dim frequency As Long = System.Diagnostics.Stopwatch.Frequency
Console.WriteLine($"Timer Frequency: {frequency} ticks per second")
输出:
ElapsedTicks
属性提供对原始计时刻度数的直接访问,无需将其转换为时间单位。 这在执行自定义计算或处理低级时间要求时很有用。
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Code segment
long elapsedTicks = stopwatch.ElapsedTicks;
Console.WriteLine($"Elapsed Ticks: {elapsedTicks}");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Code segment
long elapsedTicks = stopwatch.ElapsedTicks;
Console.WriteLine($"Elapsed Ticks: {elapsedTicks}");
Dim stopwatch As New Stopwatch()
stopwatch.Start()
' Code segment
Dim elapsedTicks As Long = stopwatch.ElapsedTicks
Console.WriteLine($"Elapsed Ticks: {elapsedTicks}")
输出:
IronPDF 是一个强大的 C# 库,让开发人员可以在其 .NET 应用程序中轻松创建、操作和处理 PDF 文档。 无论您需要从HTML、图像或其他格式生成PDF,IronPDF都提供了一整套工具,便于无缝集成到您的C#项目中。
IronPDF 提供了独特的功能,可以将HTML 转换为 PDF,并保持布局和样式完好无损。 此功能非常适合从网页内容创建PDF,如报告、发票或文档。 您可以将HTML文件、URL和HTML字符串转换为PDF文件。
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer = New ChromePdfRenderer()
' 1. Convert HTML String to PDF
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")
' 2. Convert HTML File to PDF
Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")
' 3. Convert URL to PDF
Dim url = "http://ironpdf.com" ' Specify the URL
Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
pdfFromUrl.SaveAs("URLToPDF.pdf")
End Sub
End Class
在您的C#应用程序中开始使用IronPDF,请按照以下简单步骤操作:
Install-Package IronPdf
Install-Package IronPdf
或者,您可以使用IronPDF NuGet Package Page来下载和安装“IronPdf”软件包。
using IronPdf;
using IronPdf;
Imports IronPdf
现在,您已经准备好在您的应用程序中利用 IronPDF 的功能了。
现在,我们演示如何使用C#的Stopwatch
类来测量通过使用IronPDF从URL创建PDF所需的时间:
using System;
using System.Diagnostics;
using IronPdf;
class Program
{
static void Main()
{
// Initialize IronPDF
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Specify the URL for PDF generation
string urlToConvert = "https://example.com";
// Use Stopwatch to measure the time taken
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Create PDF from URL
PdfDocument PDF = Renderer.RenderUrlAsPdf(urlToConvert);
// Stop measuring elapsed time
stopwatch.Stop();
// Save the generated PDF to a file or perform other actions
PDF.SaveAs("GeneratedPDF.pdf");
// Display the time taken
Console.WriteLine($"Time taken to create PDF from URL: {stopwatch.ElapsedMilliseconds} milliseconds");
}
}
using System;
using System.Diagnostics;
using IronPdf;
class Program
{
static void Main()
{
// Initialize IronPDF
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Specify the URL for PDF generation
string urlToConvert = "https://example.com";
// Use Stopwatch to measure the time taken
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Create PDF from URL
PdfDocument PDF = Renderer.RenderUrlAsPdf(urlToConvert);
// Stop measuring elapsed time
stopwatch.Stop();
// Save the generated PDF to a file or perform other actions
PDF.SaveAs("GeneratedPDF.pdf");
// Display the time taken
Console.WriteLine($"Time taken to create PDF from URL: {stopwatch.ElapsedMilliseconds} milliseconds");
}
}
Imports System
Imports System.Diagnostics
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Initialize IronPDF
Dim Renderer As New IronPdf.HtmlToPdf()
' Specify the URL for PDF generation
Dim urlToConvert As String = "https://example.com"
' Use Stopwatch to measure the time taken
Dim stopwatch As New Stopwatch()
stopwatch.Start()
' Create PDF from URL
Dim PDF As PdfDocument = Renderer.RenderUrlAsPdf(urlToConvert)
' Stop measuring elapsed time
stopwatch.Stop()
' Save the generated PDF to a file or perform other actions
PDF.SaveAs("GeneratedPDF.pdf")
' Display the time taken
Console.WriteLine($"Time taken to create PDF from URL: {stopwatch.ElapsedMilliseconds} milliseconds")
End Sub
End Class
此示例初始化IronPDF,使用HtmlToPdf
类从指定的URL渲染PDF,并使用Stopwatch
测量所用时间。 调整urlToConvert
变量为所需的URL,您可以根据应用程序的需要进一步自定义PDF创建过程。
输出:
总之,C#中的Stopwatch
类是精确时间测量和性能分析的关键工具,为开发人员提供优化代码和评估运行效率的手段。 直观的界面和高级功能使其在各种计时需求中具有多功能性。 此外,将IronPDF集成到C#项目中扩展了该语言在PDF文档处理方面的能力,为生成、修改和处理PDF提供了无缝解决方案。
使用Stopwatch
来测量使用IronPDF从URL创建PDF的时间的示例展示了精确时间跟踪与高级库之间的协同作用,突出了在评估应用程序性能时细致计时的重要性。 结合 C# 的 Stopwatch
和 IronPDF,开发人员能够构建具备精确计时和多功能 PDF 处理能力的高性能应用程序。
要获取免费试用许可证以测试IronPDF功能,请访问IronPDF许可信息页面。 关于URL到PDF转换的完整教程可以在IronPDF URL to PDF教程中找到。