C# 精准计时之 QueryPerformanceCounter QueryPerformanceFrequency用法
2024-09-25 06:37:06
C# 用法:
public static class QueryPerformanceMethd
{
[DllImport("kernel32.dll")]
public extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")]
public extern static short QueryPerformanceFrequency(ref long x);
}
static void Main(string[] args)
{
long stop_Value = 0;
long start_Value = 0;
long freq = 0; QueryPerformanceMethd.QueryPerformanceFrequency(ref freq);
QueryPerformanceMethd.QueryPerformanceCounter(ref start_Value);
//Fun() 需要计时方法
QueryPerformanceMethd.QueryPerformanceCounter(ref stop_Value);
double time = (double)(stop_Value - start_Value) / (double)(freq); Console.WriteLine(time);//单位S
Console.ReadLine();
}
计算所得time即为fun()方法所消耗时间。
C++中QueryPerformanceCounter QueryPerformanceFrequency的用法
#include "stdafx.h"
#include "windows.h" void main()
{
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
double time;
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nBeginTime);
Sleep();
QueryPerformanceCounter(&nEndTime);
time = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) / (double)nFreq.QuadPart;
printf("%f\n", time);
system("Pause");
}
计算Sleep(1000)所消耗的精确时间,并非精确的1s
最新文章
- CSS属性小结之--半透明处理
- FineReport层式报表解决大数据集展示问题攻略
- 【快报】基于K2 BPM的新一代协同办公门户实践交流会
- http协议本身能获取客户端Mac地址问题
- PHP 提取图片img标记中的任意属性
- bzoj2743
- FANTASY:In which way do you think the world will end?
- LVS集群TUN模式实例(5)
- 从 0 到 1 实现 React 系列 —— 5.PureComponent 实现 &;&; HOC 探幽
- 关于使用Visual编译静态库动态库及其使用的问题
- wcf事务
- 卸载CentOS7自带的OpenJDK
- PAT——1059. C语言竞赛
- flex布局语法(阮一峰)
- 灵活的javaScript
- VueJS开发所用到的技术栈
- 算法练习5---快速排序Java版
- ssh以bash登录的配置
- Git/GitHub仓库管理常用的3条命令
- Linux环境安装Nginx详细步骤
热门文章
- quarz入门案例
- linux的netstat命令详解
- ThinkPHP5如何引用新建的配置文件?
- ORA-01940:无法删除当前已链接的用户
- Hadoop之HDFS(二)HDFS基本原理
- elasticsearch2.x插件之一:kopf
- 【bzoj1602】[Usaco2008 Oct]牧场行走
- Linux 安装lamp
- 今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因
- 2-配置Andriod环境时的错误。。。Theme.AppCompat.Light