当前位置: 代码网 > it编程>编程语言>Asp.net > C#计算一段程序运行时间的多种方法总结

C#计算一段程序运行时间的多种方法总结

2025年10月22日 Asp.net 我要评论
直接代码:第一种方法利用system.datetime.nowstatic void subtest(){datetime befordt = system.datetime.now; //耗时巨大

直接代码:

第一种方法利用system.datetime.now

static void subtest()
{
	datetime befordt = system.datetime.now;  

	//耗时巨大的代码
	
	datetime afterdt = system.datetime.now;
	timespan ts = afterdt.subtract(befordt);
	console.writeline("datetime总共花费{0}ms.", ts.totalmilliseconds);
}

第二种用stopwatch类(system.diagnostics)

static void subtest()
{
	stopwatch sw = new stopwatch();
	sw.start();
  
	//耗时巨大的代码
	
	sw.stop();
	timespan ts2 = sw.elapsed;
	console.writeline("stopwatch总共花费{0}ms.", ts2.totalmilliseconds);
}

第三种用api实现: 

[system.runtime.interopservices.dllimport("kernel32.dll")]
static extern bool queryperformancecounter(ref long count);
[system.runtime.interopservices.dllimport("kernel32.dll")]
static extern bool queryperformancefrequency(ref long count);   
static void subtest()
{
	long count = 0;
	long count1 = 0;
	long freq = 0;
	double result = 0;
	queryperformancefrequency(ref freq);
	queryperformancecounter(ref count);   

	//耗时巨大的代码
	
	queryperformancecounter(ref count1);
	count = count1 - count;
	result = (double)(count) / (double)freq;
	console.writeline("queryperformancecounter耗时: {0} 秒", result);
}

方法补充

c#中获取程序执行时间的三种方法

1. 使用datetime类

你可以在程序开始执行前获取当前时间,然后在程序结束时再次获取当前时间,通过这两个时间点计算程序执行时间。

using system;
 
class program
{
    static void main()
    {
        datetime starttime = datetime.now;
 
        // 执行你的代码
        for (int i = 0; i < 1000000; i++)
        {
            // 示例:一个简单的循环
        }
 
        datetime endtime = datetime.now;
        timespan executiontime = endtime - starttime;
 
        console.writeline("程序执行时间: " + executiontime.totalmilliseconds + " 毫秒");
    }
}

2. 使用stopwatch类

system.diagnostics.stopwatch类是测量时间的一种更精确的方法,特别是对于需要高精度计时的场景。

using system;
using system.diagnostics;
 
class program
{
    static void main()
    {
        stopwatch stopwatch = new stopwatch();
        stopwatch.start();
 
        // 执行你的代码
        for (int i = 0; i < 1000000; i++)
        {
            // 示例:一个简单的循环
        }
 
        stopwatch.stop();
        console.writeline("程序执行时间: " + stopwatch.elapsedmilliseconds + " 毫秒");
    }
}

3. 使用environment.tickcount或environment.tickcount64(对于64位系统)

这种方法不如stopwatch精确,但对于简单的性能测试或快速获取时间差也是可行的。

using system;
 
class program
{
    static void main()
    {
        int starttime = environment.tickcount; // 或使用environment.tickcount64对于64位系统以避免溢出
 
        // 执行你的代码
        for (int i = 0; i < 1000000; i++)
        {
            // 示例:一个简单的循环
        }
 
        int endtime = environment.tickcount; // 或使用environment.tickcount64对于64位系统以避免溢出
        int executiontime = endtime - starttime; // 注意:这将返回以毫秒为单位的整数,但不直接提供timespan对象。
 
        console.writeline("程序执行时间: " + executiontime + " 毫秒");
    }
}

到此这篇关于c#计算一段程序运行时间的多种方法总结的文章就介绍到这了,更多相关c#计算程序运行时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com