您的位置:首页 > 编程语言 > C语言/C++

c++ 测试计算时间

2015-05-28 15:57 204 查看
/* clock example: frequency of primes */
#include <stdio.h>      /* printf */
#include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>       /* sqrt */

int frequency_of_primes (int n) {
int i,j;
int freq=n-1;
for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
return freq;
}

int main ()
{
clock_t t;
int f;
t = clock();
printf ("Calculating...\n");
f = frequency_of_primes (99999);
printf ("The number of primes lower than 100,000 is: %d\n",f);
t = clock() - t;
printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
return 0;
}
Edit & Run
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">Output:Calculating...The number of primes lower than 100,000 is: 9592It took me 143 clicks (0.143000 seconds).</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: