CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  专题开发/技术/项目 >  数据结构与算法

怎样计算C程序的运行时间

楼主redsprite()2002-11-18 16:33:47 在 专题开发/技术/项目 / 数据结构与算法 提问

需要精确道毫秒。  
  不要使用WINDOWS下的函数,最好是C或C++的基本函数,以便在UNIX下运行。  
  多谢了! 问题点数:60、回复次数:15Top

1 楼Riemann()回复于 2002-11-18 16:47:26 得分 0

在程序开始与结束时分别读取系统时间,作差即可。Top

2 楼dcyu(Dd)回复于 2002-11-18 16:51:33 得分 10

一个例子:  
  #include   <stdio.h>  
  #include   <bios.h>  
  #include   <time.h>  
  #include   <math.h>  
   
  void   main()  
  {  
    long   i;  
    float   a,b,c;  
   
    long   bios_time;  
    double   start,end;  
   
    bios_time   =   biostime(0,   0L);  
    start   =   bios_time   /   CLK_TCK;  
    a=234.32;b=187.26;  
    for(i=1;i<=100000000;i++)  
    {  
      c=a*b;  
   
    }  
   
    bios_time   =   biostime(0,   0L);  
    end   =   bios_time   /   CLK_TCK;  
    printf("Run   time:   %lf   \n",end-start);  
   
  }  
  开始运行时和结束时插入抽样时间,然后将时间相减就可以了。Top

3 楼redsprite()回复于 2002-11-18 17:12:50 得分 0

接着问:如果是WINDOWS下,不用上面的BIOS.H文件,该如何做呢?Top

4 楼dcyu(Dd)回复于 2002-11-18 20:29:02 得分 0

你不是说“不要使用WINDOWS下的函数”吗?Top

5 楼kwest(为了Job,不得不学Linux)回复于 2002-11-19 09:31:12 得分 0

我一直用的是lcc-win32,它兼容ANSI   C,可以自动显示出程序执行时间Top

6 楼redsprite()回复于 2002-11-19 15:02:51 得分 0

回:dcyu(Dd)  
  我现在调试在WINDOWS下,以后要换到UNIX下运行。所以各种情况都要考虑。  
  WINDOWS下BIOS.H文件没法用的说。  
  如果太麻烦就算了,分还是照给。Top

7 楼redsprite()回复于 2002-11-19 15:15:47 得分 0

多谢dcyu(Dd)!  
   
  Top

8 楼redsprite()回复于 2002-11-19 15:21:38 得分 0

多谢dcyu(Dd)!  
  我现在调试在WINDOWS下,以后要换到UNIX下运行。所以各种情况都要考虑。      
  WINDOWS下BIOS.H文件没法用的说。      
  Top

9 楼dxhdxh2k(dxhdxh)回复于 2002-11-19 15:30:03 得分 10

好象是getticktime()   或gettickcount(),具体你查一查Top

10 楼dcyu(Dd)回复于 2002-11-19 17:45:17 得分 0

bios.h是dos下的标准输入输出库,windows下不能用,以前记得有一个记时函数只要time.h库文件就行,但是忘了是什么了。unix下估计也不能使用biostime这个函数,你还是去查一查ANSI   C的库函数吧。Top

11 楼asvaboy1980(蓝boy)回复于 2002-11-20 17:52:33 得分 0

gzTop

12 楼feng_zq(乔)回复于 2002-11-21 11:19:41 得分 20

这段代码是一个朋友给的,我一直用它测时间很好用  
  好像是直接读的中断  
   
  只要把它加在你代码里就可以了  
   
   
  #include   <stdio.h>  
  #include   <dos.h>  
   
  unsigned   int   GetTimeCount(void)  
  {  
  union   REGS   reg;  
  reg.h.ah   =   0;  
  int86(0x1a,   &reg,   &reg);  
  return   (reg.x.dx);  
  }  
   
  void   main()  
  {  
  unsigned   int   StartTime,   EndTime;  
  StartTime   =   GetTimeCount();  
   
   
   
   
   
  EndTime   =   GetTimeCount();  
  printf("************************");  
  printf("\nThe   Tims   is:   %lfs",   (EndTime-StartTime)/18.18);  
  printf("\n************************\n\n");  
  }Top

13 楼huzhangyou(信仰(http://www.libing.net.cn))回复于 2002-11-22 15:12:20 得分 20

/*   CLOCK.C:   This   example   prompts   for   how   long  
    *   the   program   is   to   run   and   then   continuously  
    *   displays   the   elapsed   time   for   that   period.  
    */  
   
  #include   <stdio.h>  
  #include   <stdlib.h>  
  #include   <time.h>  
   
  void   sleep(   clock_t   wait   );  
   
  void   main(   void   )  
  {  
        long         i   =   600000L;  
        clock_t   start,   finish;  
        double     duration;  
   
        /*   Delay   for   a   specified   time.   */  
        printf(   "Delay   for   three   seconds\n"   );  
        sleep(   (clock_t)3   *   CLOCKS_PER_SEC   );  
        printf(   "Done!\n"   );  
   
        /*   Measure   the   duration   of   an   event.   */  
        printf(   "Time   to   do   %ld   empty   loops   is   ",   i   );  
        start   =   clock();  
        while(   i--   )    
              ;  
        finish   =   clock();  
        duration   =   (double)(finish   -   start)   /   CLOCKS_PER_SEC;  
        printf(   "%2.1f   seconds\n",   duration   );  
  }  
   
  /*   Pauses   for   a   specified   number   of   milliseconds.   */  
  void   sleep(   clock_t   wait   )  
  {  
        clock_t   goal;  
        goal   =   wait   +   clock();  
        while(   goal   >   clock()   )  
              ;  
  }  
   
   
   
  vc下面通过Top

14 楼huzhangyou(信仰(http://www.libing.net.cn))回复于 2002-11-22 15:12:55 得分 0

/*   CLOCK.C:   This   example   prompts   for   how   long  
    *   the   program   is   to   run   and   then   continuously  
    *   displays   the   elapsed   time   for   that   period.  
    */  
   
  #include   <stdio.h>  
  #include   <stdlib.h>  
  #include   <time.h>  
   
  void   sleep(   clock_t   wait   );  
   
  void   main(   void   )  
  {  
        long         i   =   600000L;  
        clock_t   start,   finish;  
        double     duration;  
   
        /*   Delay   for   a   specified   time.   */  
        printf(   "Delay   for   three   seconds\n"   );  
        sleep(   (clock_t)3   *   CLOCKS_PER_SEC   );  
        printf(   "Done!\n"   );  
   
        /*   Measure   the   duration   of   an   event.   */  
        printf(   "Time   to   do   %ld   empty   loops   is   ",   i   );  
        start   =   clock();  
        while(   i--   )    
              ;  
        finish   =   clock();  
        duration   =   (double)(finish   -   start)   /   CLOCKS_PER_SEC;  
        printf(   "%2.1f   seconds\n",   duration   );  
  }  
   
  /*   Pauses   for   a   specified   number   of   milliseconds.   */  
  void   sleep(   clock_t   wait   )  
  {  
        clock_t   goal;  
        goal   =   wait   +   clock();  
        while(   goal   >   clock()   )  
              ;  
  }  
   
   
   
  vc下面通过Top

15 楼redsprite()回复于 2002-11-26 17:40:48 得分 0

多谢楼上的各位了。只是分太少了,加点吧。Top

相关问题

  • C#一个计算密集型程序如何提高运行速度?
  • 用C#写了程序,但只能在本机运行,在其他计算机上面运行不了
  • C#程序运行问题
  • C中是否有计算程序运行多长“时间”跟使用多少“内存”的函数?
  • c/s构架的程序,计算部分要放在unix服务器上运行,怎样设计?
  • 如何在linux下运行c程序?
  • 在VC中能运行C++程序吗?
  • 关于c程序的运行。
  • 关于c#程序的运行环境
  • 关于C#程序的运行环境

关键词

  • sleep
  • sec
  • start
  • bios
  • clock
  • duration
  • gettimecount
  • goal
  • finish
  • endtime

得分解答快速导航

  • 帖主:redsprite
  • dcyu
  • dxhdxh2k
  • feng_zq
  • huzhangyou

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo