C#测试程序运行时间
一、用C#自带的StopWatch函数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace StopWatch{ class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); //这里填写要执行的代码 sw.Stop(); Console.WriteLine("总运行时间:" + sw.Elapsed); Console.WriteLine("测量实例得出的总运行时间(毫秒为单位):" + sw.ElapsedMilliseconds); Console.WriteLine("总运行时间(计时器刻度标识):" + sw.ElapsedTicks); Console.WriteLine("计时器是否运行:" + sw.IsRunning.ToString()); } }} |
运行结果如下:
总运行时间:00:00:00.0000013
测量实例得出的程序运行时间(毫秒为单位):0
总运行时间(计时器刻度标识):5
计时器是否运行:False
二、用API函数QueryPerformanceFrequency

1 usingSystem;
2
3 usingSystem.Threading;
4
5 classClass1
6
7 {
8
9 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
10
11 static extern bool QueryPerformanceCounter(ref longcount);
12
13 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
14
15 static extern bool QueryPerformanceFrequency(ref longcount);
16
17 [STAThread]
18
19 static void Main(string[] args)
20
21 {
22
23 longcount = 0;
24
25 longcount1 = 0;
26
27 longfreq = 0;
28
29 doubleresult = 0;
30
31 QueryPerformanceFrequency(reffreq);
32
33 QueryPerformanceCounter(refcount);
34
35 //需要测试的模块
36
37 intheisetoufa;
38
39 for(heisetoufa = 1; heisetoufa < 10000; heisetoufa++)
40
41 {
42
43 Console.WriteLine("第" + heisetoufa + "行");
44
45 if(heisetoufa == 5000)
46
47 {
48
49 Thread.Sleep(10000);
50
51 }
52
53 }
54
55 //需要测试的模块
56
57 QueryPerformanceCounter(refcount1);
58
59 count = count1 - count;
60
61 result = (double)(count) / (double)freq;
62
63 Console.WriteLine("耗时: {0} 秒", result);
64
65 Console.ReadLine();
66
67 }
68
69 }

C#测试程序运行时间的更多相关文章
- php测试程序运行时间和占用内存情况
php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...
- C# 测试程序运行时间和cpu使用时间
方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...
- C#中提供的精准测试程序运行时间的类Stopwatch
C#中提供的精准测试程序运行时间的类Stopwatch http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html 在需要对程序的执 ...
- C#测试程序运行时间的方法
C#测试程序运行时间的三种方法如下: (1)Datetime DateTime dtBegin = System.DateTime.Now;... DateTime dtEnnd = System.D ...
- 【VBA】测试程序运行时间,延时方法
测试程序运行时间 Dim start As Date start = Now() Dim i As Long For i = 0 To 10000000 ' 10 million Next Debug ...
- C++中几种测试程序运行时间的方法<转>
转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...
- [C.Sharp] TimeSpan的用法,获取测试程序运行时间
TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minu ...
- PHP 测试程序运行时间 microtime函数用法
PHP microtime() 函数PHP Date / Time 函数定义和用法microtime() 函数返回当前 Unix 时间戳和微秒数.语法microtime(get_as_float)参数 ...
- c/c++测试程序运行时间
算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...
随机推荐
- 【Atcoder】AGC 016 C - +/- Rectangle
[题意]给定大矩阵的边长H和W,给每格填数(<=|10^9|),要求大矩形总和为正数,而每个h*w的小矩形总和为负数,求构造方式. [算法]数学 [题解]结论题. ★当h|H&& ...
- DotNet 学习笔记 应用状态管理
Application State Options --------------------------------------------------------------------- *Htt ...
- ssh 免密码登录【用】
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...
- nginx启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...
- 从setting文件导包
两种方式 from project.settings.py import s3_key 第二种: from django.conf import setting s3_key = settings.S ...
- Windows下搭建本地SVN服务器【转】
转自:http://www.linuxidc.com/Linux/2015-01/111563.htm 本文介绍Windows下搭建本地SVN服务器的方法,网上资料比较少也比较旧,大都介绍的是旧版本S ...
- 1.tornado实现高并发爬虫
from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...
- 【 Linux 】为lnmp架构添加memcached支持
一.首先搭建lnmp平台,这里不再演示.通过php页面来进行测试如下: [root@node1 ~]# vim /usr/local/nginx/html/info.php <?php $lin ...
- 回车和换行有什么区别?我们平时按下的Enter键是回车还是换行?
来源:http://www.52rd.com/blog/Detail_RD.Blog_imjacob_12317.html -------------------------------------- ...
- KVM(三)I/O 全虚拟化和准虚拟化
在 QEMU/KVM 中,客户机可以使用的设备大致可分为三类: 1. 模拟设备:完全由 QEMU 纯软件模拟的设备. 2. Virtio 设备:实现 VIRTIO API 的半虚拟化设备. 3. PC ...