计算时间间隔

time_t 获得时间只能精确到秒,clock_t
获得时间能够精确到毫秒

#include <time.h>

clock_t start,ends;
 start=clock();

system("pause");

 ends=clock();
 cout<<ends-start<<endl;

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->#include <stdio.h>

#include <tchar.h>

#include <cstdlib>

#include <iostream>

#include <sys/timeb.h>

#include <ctime>

#include <climits>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

    //计时方式一

    time_t start = 0,end = 0;

    time(&start);

    for(int i=0; i < numeric_limits<int>::max(); i++)

    {

        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数

    }

    time(&end);

    cout << "采用计时方式一(精确到秒):循环语句运行了:" << (end-start) << "秒" << endl;

    //计时方式二

    struct timeb startTime , endTime;

    ftime(&startTime);

    for(int i=0; i < numeric_limits<int>::max(); i++)

    {

        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数

    }

    ftime(&endTime);

    cout << "采用计时方式二(精确到毫秒):循环语句运行了:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;

    //计时方式三

    clock_t startCTime , endCTime;  

    startCTime = clock();   //clock函数返回CPU时钟计时单元(clock tick)数,还有一个常量表示一秒钟有多少个时钟计时单元,可以用clock()/CLOCKS_PER_SEC来求取时间

    for(int i=0; i < numeric_limits<int>::max(); i++)

    {

        double circle = 3.1415962*i;  //浮点运算比较耗时,循环最大整数次数

    }

    endCTime = clock();

    cout << "采用计时方式三(好像有些延迟,精确到秒):循环语句运行了:" << double((endCTime-startCTime)/CLOCKS_PER_SEC) << "秒" << endl;

    cout << "综合比较上述三种种计时方式,方式二能够精确到毫秒级别,比方式一和三都较好。此外在Windows API中还有其他的计时函数,用法都大同小异,在此就不做介绍了。" << endl;

    system("pause");

    return 0;

}
时间: 2024-08-05 10:28:17

计算时间间隔的相关文章

Powershell计算时间间隔(New-TimeSpan)

在Windows PowerShell里New-TimeSpan cmdlet提供了一种方法做日期算法. 计算时间间隔: 这个命令告诉你今天的日期与2006年除夕之间的天数: New-TimeSpan $(Get-Date) $(Get-Date –month 12 -day 31 -year 2006) New-TimeSpan $(Get-Date) $(Get-Date –month 12 -day 31 -year 2006 -hour 23 -minute 30) 计算从2013/11

Mysql 计算时间间隔函数

#计算两个时间的间隔 #计算间隔天数 select TIMESTAMPDIFF(day,'2014-06-01',date(now())) #计算间隔月数 select TIMESTAMPDIFF(month,'2014-01-01',date(now())) #计算间隔年数 select TIMESTAMPDIFF(year,'2010-01-01',date(now()))Mysql 计算时间间隔函数,布布扣,bubuko.com

计算时间间隔的js

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <script> function daysBetween(DateOne,DateTwo) { var OneMonth = DateOne.substring(5,DateOne.lastIndexO

iOS NSDate计算时间间隔

//获取开始时间 NSDate* tmpStartData = [NSDate date]; /*( 执行代码段 )*/ for (int i = 0; i<10000; i++) { DLog(@"%d",i); } //计算代码段所用时间 double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData]; NSLog(@"时间间隔 %f 秒", deltaTime);

C#计算时间间隔和时间所属区间的通用操作方法

在.net项目中,对DateTime的相关操作使用的比较多,例如时间格式的转换,时间间隔的计算,时间所属的区间计算等,在这些要求中,虽然使用起来较为的简单,但是在转换的过程中,较为容易出错,花费的时间也较多,现在总结一些常用的时间操作方法,以便在项目开发中节省时间. 一下代码中需要使用到如下的实体类:         /// <summary>         /// 周(星期)信息实体类         /// </summary>         [Serializable]

计算时间间隔天数

+ (NSInteger)calculateDateInterval:(NSString *)timestamp { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [gregorian setFirstWeekday:2]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [d

windows下的计算时间间隔 -- GetTickCount()

用法: #include "windows.h" DWORD lastTime =0;DWORD currentTime = 0;DWORD spendTime = 0; lastTime  = GetTickCount();//ms //延时 currentTime = GetTickCount();spendTime = currentTime - lastTime;//ms https://blog.csdn.net/wuxinliulei/article/details/123

iOS开发之计算两个日期的时间间隔

//首先创建格式化对象  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //然后创建日期对象 NSDate *date1 = [dateFormatter dateFromString:@"2020-10-31 00:00:00"]; NSDate *date = [NSDat

C#接口性能测试--计算执行时间

在做程序的时候,肯定会遇到给他人提供接口,或者使用他人接口的地方.对于一个开发者来说,不管是提供给他人的接口还是,自己提供给他人的接口.都要进行测试. 对于很多的测试,需要详细的记录 该接口的时间范围,比如 该接口的性能为   10ms 100ms的地方. 既然要了解每个接口的性能,该测试不是为了 测试接口的正确性,只是在测试正确性的时候 同时,记录一下该接口的执行时间. 最近,因为要开发一个新项目,而且新项目中会用到旧系统的接口.所以需要对旧系统提供的接口进行测试,进而决定,里面的接口是否需要