C#测试程序运行时间的三种方法如下:

(1)Datetime

DateTime dtBegin = System.DateTime.Now;
...
DateTime dtEnnd = System.DateTime.Now;
TimeSpan dtTime = dtEnnd- dtBegin;

(2)Stopwatch

需要引用System.Diagnostics命名空间

Stopwatch sw = new Stopwatch();
sw.Start();
...
sw.Stop();
TimeSpan swTime = sw.Elapsed;

(3)Process.GetCurrentProcess()

TimeSpan gcpBegin = Process.GetCurrentProcess().TotalProcessorTime;
...
TimeSpan gcpTime = Process.GetCurrentProcess().TotalProcessorTime.Subtract(gcpBegin);

问题一:我的CPU数量是4,那计算CPU时间的话,需不需要除以4呢?

问题二:三种方法得到的时间是不同的,Datetime和Stopwatch得到的时间几乎一样,而Process.GetCurrentProcess()得到的时间远小于前两种,不是4倍关系,这又是为什么呢?

目前还没有找到答案,望高手指点一二。

C#测试程序运行时间的方法的更多相关文章

  1. C++中几种测试程序运行时间的方法<转>

    转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...

  2. 【VBA】测试程序运行时间,延时方法

    测试程序运行时间 Dim start As Date start = Now() Dim i As Long For i = 0 To 10000000 ' 10 million Next Debug ...

  3. C# 测试程序运行时间和cpu使用时间

    方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...

  4. php测试程序运行时间和占用内存情况

    php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...

  5. C#中提供的精准测试程序运行时间的类Stopwatch

    C#中提供的精准测试程序运行时间的类Stopwatch http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html 在需要对程序的执 ...

  6. C++获取当前时间和计算程序运行时间的方法

    C++获取当前时间和计算程序运行时间的方法 获取当前时间: #include <iostream> #include <Windows.h> using namespace s ...

  7. 三种计算c#程序运行时间的方法

    三种计算c#程序运行时间的方法 第一种: 利用 System.DateTime.Now // example1: System.DateTime.Now method DateTime dt1 = S ...

  8. 在Windows及Linux下获取毫秒级运行时间的方法

    在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...

  9. c# 三种计算程序运行时间的方法

    三种计算c#程序运行时间的方法第一种:利用 System.DateTime.Now // example1: System.DateTime.Now method DateTime dt1 = Sys ...

随机推荐

  1. 11、OpenCV Python 图像金字塔

    __author__ = "WSX" import cv2 as cv import numpy as np # 高斯金字塔 #金字塔 原理 ==> 高斯模糊+ 降采样 #金 ...

  2. WebForm与MVC混用 (转)

    http://blog.csdn.net/leftfist/article/details/11591231

  3. Tensorflow实践

    确定文件的编码格式 # -*- coding : utf-8 -*- 引入tensorflow库 import tensorflow as tf 定义常量 hw=tf.contant("he ...

  4. HttpServletResponse 解决中文乱码

    response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setChara ...

  5. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_值类型的装箱和拆箱(二)

    [注意]:如果知道自己写的代码会造成编译器反复对一个值类型进行装箱,请改成用手动方式对值类型进行装箱. [好处]:代码会变得更小.更快. [例子]: using System; public seal ...

  6. 毕业设计 python opencv实现车牌识别 预处理

    主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...

  7. Educational Codeforces Round 3 C

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 为经典版eclipse增加web and Java EE插件

    http://download.eclipse.org/releases/luna/ 0 1 2选择对应版本“luna”,http://download.eclipse.org/releases/lu ...

  9. CSS文件的三种引入方式

    CSS的引入方式共有三种:行内样式.内部样式表.外部样式表. 一.行内样式 使用style属性引入CSS样式. 示例:<h1 style="color:red;">st ...

  10. POI 读大文件日志

    POI的三个目录 usermodel 包含很多类,方便用户使用,但是占用内存大 eventusermodel 使用xml的SAX事件解析,XSSFReader创建时必须使用OPCPackage,pkg ...