惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 光光GG

最小生成树:使用堆和并查集的kruskal算法 最小生成树prim算法模板 【转】详细解说STL hash_map系列 松弛操作 SPFA算法 bellman-ford(贝尔曼-福特)算法 详细解说 STL 排序(Sort) c++常用算法收集 骨牌覆盖的已有研究 【转】自定义排序函数实现时需要注意的问题 【原】八数码问题(8-puzzle Problem) 解决netbeans乱码问题 在ubuntu8.04 stl vector 技巧 ACM中的64位整数 pku-1095-Trees Made to Order 七种qsort排序方法 memset ,memcpy sql server 通过mdf文件恢复数据库 产生满足正态分布的随机数
【转】计算C++程序运行时间
光光GG · 2008-10-07 · via 博客园 - 光光GG

C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:  
   
   
   
  clock_t   clock(   void   );  
   
   
   
  这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock   tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义:  
   
   
   
  #ifndef   _CLOCK_T_DEFINED  
  typedef   long   clock_t;  
  #define   _CLOCK_T_DEFINED  
  #endif  
   
   
   
  很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:  
   
   
   
  #define   CLOCKS_PER_SEC   ((clock_t)1000)  
   
   
   
  可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:  
   
   
   
  void   elapsed_time()  
  {  
  printf("Elapsed   time:%u   secs."n",clock()/CLOCKS_PER_SEC);  
  }  
   
   
   
  当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:  
   
   
   
  #include   “stdio.h”  
  #include   “stdlib.h”  
  #include   “time.h”  
   
   
   
  int   main(   void   )  
  {  
        long         i   =   10000000L;  
        clock_t   start,   finish;  
        double     duration;  
        /*   测量一个事件持续的时间*/  
        printf(   "Time   to   do   %ld   empty   loops   is   ",   i   );  
        start   =   clock();  
        while(   i--   )             ;  
        finish   =   clock();  
        duration   =   (double)(finish   -   start)   /   CLOCKS_PER_SEC;  
        printf(   "%f   seconds"n",   duration   );  
        system("pause");  
  }  
   
   
   
  在笔者的机器上,运行结果如下:  
   
   
   
  Time   to   do   10000000   empty   loops   is   0.03000   seconds  
   
   
   
  上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一 些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。