Windows 各种计时函数总结

windows下的定时器(获取系统时间)和linux下的定时器(获取系统时间) 一、windows下的clock(): #include <stdio.h> #include <time.h> #include <windows.h> int main() { printf("startTime:%ld\n",clock()); Sleep(50);//50ms printf("endTime:%ld\n",clock()); r... 阅读详情
本文对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows系统,也可以用于Linux系统。在Windows系统下三种,使用Windows提供的API接口timeGetTime()、GetTickCount()及QueryPerformanceCounter()来完成。文章最后给出了5种计时方法示例代码。

 

标准C/C++的二个计时函数time()及clock()

 

time_t time(time_t *timer);

返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

time_t实际是个long长整型typedef long time_t;

头文件:#include <time.h>

 

clock_t clock(void);

返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。

clock_t实际是个long长整型typedef long clock_t;

头文件:#include <time.h>

 

 

Windows系统API函数

timeGetTime()、GetTickCount()及QueryPerformanceCounter()

 

DWORD timeGetTime(VOID);

返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。

头文件:#include <Mmsystem.h>            

引用库:#pragma comment(lib, "Winmm.lib")  

 

DWORD WINAPI GetTickCount(void);

这个函数和timeGetTime()一样也是返回系统时间,以毫秒为单位。

头文件:直接使用#include <windows.h>就可以了。

 

高精度计时,以微秒为单位(1毫秒=1000微秒)。

先看二个函数的定义

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

得到高精度计时器的值(如果存在这样的计时器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

返回硬件支持的高精度计数器的频率(次每秒),返回0表示失败。

再看看LARGE_INTEGER

它其实是一个联合体,可以得到__int64 QuadPart;也可以分别得到低32位DWORD LowPart和高32位的值LONG HighPart。

在使用时,先使用QueryPerformanceFrequency()得到计数器的频率,再计算二次调用QueryPerformanceCounter()所得的计时器值之差,用差去除以频率就得到精确的计时了。

头文件:直接使用#include <windows.h>就可以了。

下面给出示例代码,可以在你电脑上测试下。

本文对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows系统,也可以用于Linux系统。在Windows系统下三种,使用Windows提供的API接口timeGetTime()、GetTickCount()及QueryPerformanceCounter()来完成。文章最后给出了5种计时方法示例代码。
 

标准C/C++的二个计时函数time()及clock()

 

time_t time(time_t *timer);

返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

time_t实际是个long长整型typedef long time_t;

头文件:#include <time.h>

 

clock_t clock(void);

返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。

clock_t实际是个long长整型typedef long clock_t;

头文件:#include <time.h>

 

 

Windows系统API函数

timeGetTime()、GetTickCount()及QueryPerformanceCounter()

 

DWORD timeGetTime(VOID);

返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。

头文件:#include <Mmsystem.h>            

引用库:#pragma comment(lib, "Winmm.lib")  

 

DWORD WINAPI GetTickCount(void);

这个函数和timeGetTime()一样也是返回系统时间,以毫秒为单位。

头文件:直接使用#include <windows.h>就可以了。

 

高精度计时,以微秒为单位(1毫秒=1000微秒)。

先看二个函数的定义

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

得到高精度计时器的值(如果存在这样的计时器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

返回硬件支持的高精度计数器的频率(次每秒),返回0表示失败。

再看看LARGE_INTEGER

它其实是一个联合体,可以得到__int64 QuadPart;也可以分别得到低32位DWORD LowPart和高32位的值LONG HighPart。

在使用时,先使用QueryPerformanceFrequency()得到计数器的频率,再计算二次调用QueryPerformanceCounter()所得的计时器值之差,用差去除以频率就得到精确的计时了。

头文件:直接使用#include <windows.h>就可以了。

 

 

下面给出示例代码,可以在你电脑上测试下。

别再纠结clock_gettime了!Windows下用QueryPerformanceCounter实现高精度计时(附完整代码示例) 本文详细介绍了在Windows平台下使用QueryPerformanceCounter(QPC)实现高精度计时的方法,替代Linux中的clock_gettime(CLOCK_MONOTONIC)。通过解析QPC的核心API、进阶技巧和性能优化实践,帮助开发者掌握跨平台计时方案,提升应用程序的时间测量精度。 阅读详情

相关推荐

yakit 验证码识别 进行webfuzzer

在渗透测试的过程中,我们可能需要对管理员密码进行枚举,或者密码喷洒的时候,遇到验证码就很烦。有没有自动化识别验证码的办法呢?有的,兄弟,有的。验证码的识别率很低,captcha-killer-modified对算术验证码识别正确率非常低,还会识别成乱七八糟的结果,这就非常麻烦。因为你不知道到底是验证码不对,还是密码不对。这是 ddddocr 识别率的问题,如果师傅们找到准确度更高的工具,完全可以替换上去。

Arkmornings的博客 1664

Windows上的类似clock_gettime(CLOCK_MONOTONIC)的高精度测量时间函数

一般在 x86 架构设备上, QPC 来测量时间是通过访问处理器的的 TSC(时间戳计数器)来实现的,不过某些设备的 BIOS 可能不能正确设置 CPU 特性,比如设置成可变 TSC,那这个就会受其他一些因素的影响了。虽然主要是使用 TSC 实现的,但是微软官方不建议使用使用 RDTSC/RDTSCP(后者多一个指定 CPU)来直接获取 TSC 信息,因为这样软件程序的兼容性会大大降低(比如说程序运行在可变 TSC 或者没有 TSC 的设备系统上,代码可能无法运行或者误差较大)。用来转换单位,表示微秒。

ZhongUncle的博客 3312

Cocos Creator Android打包:NDK版本选择与配置全攻略

在Android原生开发中,NDK(Native Development Kit)是编译C/C++代码的核心工具链,它决定了代码如何被编译为不同CPU架构(如ARMv7、ARM64)的动态库。其工作原理在于提供了一套完整的交叉编译环境,将高级语言代码转化为设备可执行的二进制指令。这一技术对于游戏引擎和性能敏感型应用至关重要,因为它能直接调用硬件资源,提升运行效率。在实际工程中,NDK版本必须与Android Gradle插件(AGP)版本、项目依赖的C++标准库精密匹配,否则会导致编译失败或运行时崩溃。特别

weixin_33725270的博客 490

【C语言】clock_gettime函数的使用

第一个参数有下面几种选项。

慕雪华年的博客 2678

OpenClaw 聪明设置指南(真实可用方案)

本文介绍了OpenClaw AI系统的配置与优化指南,主要内容包括:1)基础配置,涵盖初始化流程、安全设置和配置文件位置;2)模型分层策略,推荐主力模型与降级方案;3)性能优化,包括推理加速和内存管理;4)智能提升方法,如人格定义和技能安装;5)高级功能如多Agent协同和Token优化。文章提供了详细的配置命令和参数说明,帮助用户根据需求平衡性能与成本,实现高效AI助手部署。

qq_43344878的博客 708

Apollo&Carla联合仿真环境搭建与实战调试

本文详细介绍了Apollo与Carla联合仿真环境的搭建与实战调试流程,涵盖硬件配置、软件依赖安装、Apollo环境配置、Carla仿真器安装、桥接服务设置以及常见问题解决方案。通过逐步指导,帮助开发者高效实现自动驾驶仿真测试,提升开发效率。

ee345的博客 200

标准库:time(windows用)

time.asctime([t]) 转换由gmtime()或localtime()所返回的表示时间的元组或struct_time为以下形式的字符串:'SunJun2023:21:051993'。 如果未提供t,则会使用localtime()所返回的当前时间。asctime()不会使用区域设置信息。 import time print(time.asctime()) # output: Fri Apr 8 11:04:28 2022 time.ctime([sec...

hd51cc的博客 625

Global Mapper与ArcGIS双剑合璧:自动化与半自动矢量化提取地物轮廓实战

本文详细介绍了如何结合Global Mapper的自动化批量提取与ArcGIS的半自动精细修整功能,高效完成地物轮廓矢量化工作。通过实战案例,阐述了利用Global Mapper快速抓取初步轮廓,再借助ArcGIS的ArcScan等工具进行精细化编辑和优化的完整工作流,有效提升地理信息数据处理效率。

weixin_29284657的博客 119

windows各种计时函数总结

本文对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows系统,也可以用于Linux系统。在Windows系统下三种,使用Windows提供的API接口timeGetTime()、GetTickCount()及QueryPerf

暮风 882

AI大模型驱动Web UI自动化测试:Playwright与RobotFramework智能整合实践

Web自动化测试是软件质量保障的关键环节,其核心原理是通过脚本模拟用户操作,对Web应用界面进行功能验证。传统方法依赖固定的元素定位和脚本逻辑,在应对频繁的UI变更时面临维护成本高、脚本脆弱的挑战。随着AI大模型技术的发展,其强大的自然语言理解和代码生成能力为自动化测试带来了新的技术价值。通过将大模型的意图理解与代码生成能力,与Playwright强大的浏览器自动化执行引擎以及RobotFramework灵活的关键字驱动框架相结合,可以构建出能够理解自然语言指令、动态生成稳定操作脚本的智能测试系统。这种融合

weixin_28718769的博客 412

基于YOLO26深度学习框架的文档表格识别系统的算法研究实现方法

本文研究了基于YOLO26深度学习框架的文档表格识别系统实现方法。系统采用YOLO26网络架构,通过数据预处理、模型训练、检测推理和表格分析四个模块实现高效表格识别。研究详细阐述了数据归一化、增强等技术处理,以及采用迁移学习策略优化模型训练过程。实验表明,该系统能有效识别多种表格结构并生成结构化数据,为文档自动化处理提供了可靠解决方案。

一键难忘的博客 971

Windows计时器函数

文章目录1、标准c/c++函数(windows/Linux)2、windows系统提供的API接口 1、标准c/c++函数(windows/Linux) 1、clock()函数,毫秒精度 #include <time.h> clock_t time_begin= clock(); Sleep(1000); clock_t time_end = clock(); std::cout &...

Joker_N的博客 1311

【ubuntu24.04】auto remove后,重启,kernel panic ,please reboot your computer vfs unable to mount root fs

Ubuntu 24.04系统在运行auto remove后出现内核恐慌(kernel panic)无法启动,显示"VFS: unable to mount root fs"错误。该问题是由于当前内核被清除导致系统无法挂载根分区。解决步骤包括:1)尝试用GRUB菜单中的旧内核启动;2)在GRUB中临时编辑启动参数;3)使用Ubuntu启动盘进行chroot修复;4)使用Boot-Repair工具一键修复。成功启动后,需要重新安装linux-generic等元包,重建initramfs并更新

突围 1209

Windows与Linux下系统计时函数整理总结

通用的C/C++计时函数 time_t time(time_t *timer); 返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。 time_t实际是个long长整型typedef long time_t。 头文件:#include&lt;time.h&gt; 精度:1s   clock_t clock(void); 返回从“开...

wujiahao2016的博客 968

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 经典线程同步总结 关键段 事件 互斥量 信号量
下一篇: 计算机程序设计的史诗TAOCP
雪岢奇
博客等级 码龄17年 0粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值