能自己写函数就别调用库函数 后者时间耗时更多

#include <ctime>
#include <iostream>
#include <cmath>

using namespace std;
const int N = 1024;

int main(void)
{
    int i, M;
    time_t t1 = time(NULL);
    for(int r = 0; r < 100000000; r++)
        for(i=N,M=1;(i=i/2)!=1;M++);
    time_t t2 = time(NULL);
    for(int r = 0; r < 100000000; r++)
       M = log(N)/log(2);
    time_t t3 = time(NULL);
    cout << "time1:" << t2 - t1 << "\ntime2:" << t3 - t2 << endl; 
    return 0;
}

/*
 * res:
 * time1:4
 * time2:13
 */
posted @ 2018-07-11 14:55  cloudren2020  阅读(94)  评论(0编辑  收藏  举报