(原創) 如何對程式片段進行效能測試(benchmark) (C/C++) (C)

 1/* 
 2(C) OOMusou 2006 http://oomusou.cnblogs.com
 3
 4Filename    : Benchmark.cpp
 5Compiler    : Visual C++ 8.0
 6Description : Demo how to caculate processing time of specified function.
 7*/

 8#include <iostream>
 9#include <ctime>
10
11void sub();
12
13int main() {
14  // clock_t is typedef of (long)
15  clock_t t = clock(); 
16
17  sub();
18
19  t = clock() -t;
20
21  // CLOCKS_PER_SEC is macro
22  std::cout << "Process time:" << (double)t/CLOCKS_PER_SEC << " sec (" << t << " clocks)" << std::endl;
23  
24  return 0;
25}

26
27void sub() {
28  for(int i=0; i != 10000++i) {
29    for(int j=0; j != 100000++j) {
30      ;
31    }

32  }

33}

See Also
(原創) 如何對程式片段進行效能測試(benchmark) (.NET) (C#)

Reference
C程式設計 500個應用範例技巧大全集,平田 豐,P.468

posted on 2006-11-08 00:07  真 OO无双  阅读(1367)  评论(0编辑  收藏  举报

导航