zoukankan      html  css  js  c++  java
  • linux计算程序运行时间

    转自:

    http://www.cnblogs.com/NeilHappy/archive/2012/12/08/2808417.html


    #include <sys/time.h>
    int gettimeofday(struct timeval *tv,struct timezone *tz);
    strut timeval {
    long tv_sec; /* 秒数 */
    long tv_usec; /* 微秒数 */
    };
    gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替。

    以下是程序:
    #i nclude <sys/time.h>
    #i nclude <stdio.h>
    #i nclude <math.h>
    void function()
    {
    unsigned int i,j;
    double y;
    for(i=0;i<1000;i++)
    for(j=0;j<1000;j++)
    y=sin((double)i);
    }
    main()
    {
    struct timeval tpstart,tpend;
    float timeuse;
    gettimeofday(&tpstart,NULL);
    function();
    gettimeofday(&tpend,NULL);
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+
    tpend.tv_usec-tpstart.tv_usec;               //注意毫秒和微妙,写错了输出结果就是零了
    timeuse/=1000000;
    printf("Used Time:%f ",timeuse);
    exit(0);
    }

    //这个程序在我的电脑运行的结果大概为0.03 - 0.04s
  • 相关阅读:
    铺地毯
    解方程
    引水入城
    10.16今日暂时停更博客
    聪明的质监员
    CCF NOI plus 201(7)6 初赛题 解题报告
    初赛可能会用到的计算机基础理论知识整理
    火柴排队
    借教室
    10.10今日暂时停更博客
  • 原文地址:https://www.cnblogs.com/ediszhao/p/4162266.html
Copyright © 2011-2022 走看看