找回密码
 注册
Simdroid-非首页
查看: 301|回复: 6

[3. Fortran] 【讨论】如何在Fortran程序中计算该程序的运行时间

[复制链接]
发表于 2004-5-16 08:47:10 | 显示全部楼层 |阅读模式 来自 湖北武汉
用Fortran语言编了一段程序。
现在想知道该程序从开始运行到最后结束共运行了多少时间?
也就是希望程序结束后生成一输出文件,该文件记录了该程序的运行时间。
不知道在Fortran中如何实现这一功能?
另外,在C语言中又是如何来实现的?
发表于 2004-5-16 09:37:23 | 显示全部楼层 来自 美国

回复: 【讨论】如何在Fortran程序中计算该程序的运行时间

Simdroid开发平台
如果用windows NT或2000以上操作系统,可以用etime或dtime比较精确地得到程序运行时间。
  
DTIME (WNT only)
Portability Function: Returns the elapsed CPU time since the start of program execution when first called, and the elapsed execution time since the last call to DTIME thereafter. This function is currently restricted to WNT systems.
  
Module: USE DFPORT  
  
Syntax  
  
result = DTIME (tarray)  
  
tarray  
(Output) REAL(4). Must be a rank one array with two elements:  
  
tarray(1) Elapsed user time, which is time spent executing user code. This value includes time running protected Windows subsystem code.  
  
tarray(2) Elapsed system time, which is time spent executing privileged code (code in the Windows Executive).  
Results:  
  
The result type is REAL(4). The result is the total CPU time, which is the sum of tarray(1) and tarray(2).  
  
ETIME (WNT only)
Portability Function: Returns the elapsed CPU time, in seconds, of the process that calls it. This function is currently restricted to WNT systems.
  
Module: USE DFPORT  
  
Syntax  
  
result = ETIME (array)  
  
array  
(Output) REAL(4). Must be a rank one array with two elements:  
  
array(1) Elapsed user time, which is time spent executing user code. This value includes time running protected Windows subsystem code.  
  
array(2) Elapsed system time, which is time spent executing privileged code (code in the Windows Executive).  
Results:  
  
The result is of type REAL(4). The result is the total CPU time, which is the sum of array(1) and array(2).

评分

1

查看全部评分

发表于 2004-5-16 09:39:30 | 显示全部楼层 来自 美国

回复: 【讨论】如何在Fortran程序中计算该程序的运行时间

c语言的话请参见http://www.codeproject.com/datetime/的一些文章。
 楼主| 发表于 2004-5-17 09:35:21 | 显示全部楼层 来自 湖北武汉

回复: 【讨论】如何在Fortran程序中计算该程序的运行时间

谢谢斑竹。
我这就试试看。
发表于 2004-5-23 14:21:49 | 显示全部楼层 来自 黑龙江哈尔滨

回复: 【讨论】如何在Fortran程序中计算该程序的运行时间

一般记时使用固有函数cpu_time就行了。dtime,etime都是cvf的扩展。
  
REAL time_begin, time_end
  ...
  CALL CPU_TIME ( time_begin )
  !
  !task to be timed
  !
  CALL CPU_TIME ( time_end )
 &nbspRINT *, 'Time of operation was ', time_end - time_begin, ' seconds'
  
----
  
附加一句, cpu_time是f95中的固有函数,之前的版本都没有。

评分

1

查看全部评分

发表于 2004-5-23 21:57:31 | 显示全部楼层 来自 美国

回复: 【讨论】如何在Fortran程序中计算该程序的运行时间

苍山负雪说得对,用cpu_time具有通用性。如下是关于计时的详细技术文档,其中关于非编程使用批处理文件的方法很有意思:  
   
Methods of Timing Your Application
  
To perform application timings, use a version of the TIME command in a .BAT file (or the function timing profiling option). You might consider modifying the program to call routines within the program to measure execution time (possibly using conditionally compiled lines). For example:
  
1. Compaq Fortran intrinsic procedures, such as CPU_TIME, SYSTEM_CLOCK, DATE_AND_TIME, and TIME.  
    Library routines, such as ETIME or TIME.  
2. Visual Fortran programs created in a Windows 98, Windows Me, or Windows 95 development environment can be run and analyzed on Windows NT 4 or Windows 2000 systems. Whenever possible, perform detailed performance analysis on a system that closely resembles the system(s) that will used for actual application use.  
  
Sample Command Procedure that Uses TIME and Performance Monitor
  
The following example shows a .BAT command procedure that uses the TIME command and the Performance Monitor (perfmon) tool available on Windows NT 4 and Windows 2000 systems. The kill command that stops the perfmon tool is included on the Windows NT Resource kit; if the kill tool is not available on your system, manually end the perfmon task by using the task manager.  
  
This .BAT procedure assumes that the program to be timed is myprog.exe.  
  
Before using this batch file, start the performance monitor to setup logging of the statistics that you are interested in:  
  
1. At the DOS prompt type: Perfmon  
2. In the View menu, select Log  
3. In the Edit menu, select Add to Log and select some statistics  
4. In the Options menu, select Log. In the dialog box:
  
       a. Name the log file. The following .BAT procedure assumes that
          you have named the logfile myprog.log.  
       b. Consider adjusting the Log Interval.  
       c. As the last step, be sure to select "Start Log".  
  
5. In the File menu, select Save Workspace to save the setup information. The following .BAT procedure assumes you have saved the workspace as my_perfmon_setup.pmw.  
  
The command procedure follows:
  1.   echo off
  2.   rem  Sample batch file to record performance statistics for later analysis.
  3.   rem  This .bat file assumes that you have the utility "kill" available, which
  4.   rem  is distributed with the NT resource kit.
  5.   
  6.   rem Delete previous logs, then start up the Performance Monitor.
  7.   rem We use start so that control returns instantly to this batch file.
  8.   del myprog.log
  9.   start perfmon my_perfmon_setup.pmw
  10.   
  11.   rem print the time we started
  12.   time <nul | findstr current
  13.   
  14.   rem start the program we are interested in, this time using
  15.   rem cmd /c so that the batch file waits for the program to finish.
  16.   echo on
  17.   cmd /c myprog.exe
  18.   echo off
  19.   
  20.   rem print the time we stopped
  21.   time <nul | findstr current
  22.   
  23.   rem all done logging statistics
  24.   kill perfmon
  25.   rem if kill is not available, end the perfmon task manually
复制代码

After the run, analyze your data by using Performance Monitor:  
  
1. If it is not currently running, start Performance Monitor.  
2. In the View menu, select Chart.  
3. In the Options menu, select Data From and specify the name of
    the logfile.  
4. In the Edit menu, select Add To Chart to display the counters.

评分

1

查看全部评分

发表于 2008-12-13 10:55:13 | 显示全部楼层 来自 北京
受益匪浅,谢谢诸位大牛们
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-11-1 13:23 , Processed in 0.047736 second(s), 19 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表