private void ShowRunTime()
{
TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime;
Stopwatch stw = new Stopwatch();
stw.Start();
int Circles = 1000;
for (int i = 0; i < Circles; ++i)
{
Console.WriteLine(i.ToString());
}
double Msecs = Process.GetCurrentProcess().TotalProcessorTime.Subtract(ts1).TotalMilliseconds;
stw.Stop();
Console.WriteLine(string.Format("循环次数:{0} CPU时间(毫秒)={1} 实际时间(毫秒)={2}", Circles, Msecs, stw.Elapsed.TotalMilliseconds, stw.ElapsedTicks));
Console.WriteLine(string.Format("1 tick = {0}毫秒", stw.Elapsed.TotalMilliseconds / stw.Elapsed.Ticks));
}

核心代码:

Stopwatch stw = new Stopwatch();
stw.Start();

string aa=  wactch.ElapsedMilliseconds.ToString();

string aa=  stw.Elapsed.TotalMilliseconds.ToString();

stw.Stop();

C#实现测量程序运行时间及cpu使用时间的更多相关文章

  1. C#测量程序运行时间及cpu使用时间

    转载:http://www.cnblogs.com/yanpeng/archive/2008/10/15/1943369.html 对一个服务器程序想统计每秒可以处理多少数据包,要如何做?答案是用处理 ...

  2. C#测量程序运行时间及cpu使用时间实例方法

    private void ShowRunTime() { TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime; Stopwatc ...

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

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

  4. C# 测量程序运行时间

    using System.Diagnostics; Stopwatch watch = new Stopwatch(); watch.Start(); /* 需要测量运行时间的程序 */ watch. ...

  5. 测量C++程序运行时间

    有个很奇怪的现象,我自认为写得好的文章阅读量只有一百多,随手写的却有一千多--要么是胡搞,要么是比较浅显.纵观博客园里众多阅读过万的文章,若非绝世之作,则必为介绍入门级知识的短文.为了让我的十八线博客 ...

  6. [daily]使用rdtsc指令,测量程序的运行速度 [转]

    原文地址:http://blog.chinaunix.net/uid-24774106-id-2779245.html 最近搞架构,一直在讨论.听人提到,自行科普了一下,先转发,mark.有机会深入学 ...

  7. C/C++下测量函数运行时间

    C/C++下测量函数运行时间 time.h介绍 C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t. clock_t clock( void ); 这个函数返回从" ...

  8. VC++程序运行时间测试函数

    0:介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录函数或者算 ...

  9. C++程序运行时间-ZZ

    [15.5.25]贴一段实用的代码,linux下跑过. #include <stdio.h> /* printf */ #include <time.h> /* clock_t ...

随机推荐

  1. Qt5-控件-QRadioButton-单选按钮-用于从多个选项中选取一个-单选神器

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QRadioButton> ...

  2. android WIFI的一些属性

    package com.example.wifitest; import java.util.List; import android.content.Context; import android. ...

  3. javascript sort()与reverse()

    javascript 中提供了两个对数据进行排序的方法,即sort()和reverse() 在理解的时候犯了一个非常低级的错误,现记录如下: reverse()不包括排序的功能,只是把原来的数组反转. ...

  4. JavaScript forEach方法

    最近看了一些html5和js方面的书,受益匪浅,因为看的东西比较多,却都没有怎么静心来做整理,慢慢来吧,可能最近自己有点儿小紧张.今天跟大家分享下JavaScript的forEach方法(其实是从&l ...

  5. 【创建型】Builder模式

    生成器模式的主要思想:将产品对象的创建与表现分离开,并且同样的创建过程可以有不同的产品表现. 直白一点可以理解为:待创建的对象是复杂的,一般情况下是需要经过多个步骤的创建后,最终才能将完整产品创建好, ...

  6. Windows坐标系

    .逻辑坐标 逻辑坐标是独立于设备的,它与设备点的大小无关.使用逻辑单位,是实现"所见即所得"的基础.当程序员在调用一个画线的GDI函数LineTo,画出25.4mm(1英寸) 长的 ...

  7. WebAPI 用户认证防篡改实现HMAC(二)签名验证 AbsBaseAuthenticationAttribute--转

    public class ActionFilter : ActionFilterAttribute      {          public override void OnActionExecu ...

  8. android 截取指定位置字符串

    String str = "s\ziyuan"; String newStr = str.subString(str.indexOf("\\"),str.len ...

  9. 64位linux中使用inet_ntoa报错处理

    最近一直使用linux mint 15,我用的是64位操作系统,在进行网络编程的时候,发现一个问题,请看源码: /*get_ip_by_name.c*/ #include <stdio.h> ...

  10. python调用shell, shell 引用python

    python 调用 shell get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###get the lines of & ...