C + + Computational program run time

Source: Internet
Author: User

Reprint http://blog.csdn.net/trustbo/article/details/10582287

I used to hear people talk about how to calculate the program run time, give a series of functions, then did not notice, randomly selected clock () The simplest way to calculate. Wait until the real need to detect the performance of the program to improve how much, only to find that there is a lot to pay attention to the place.

The most common way: #includetime_t start = Clock (), time_t end = Clock ();p rintf ("The Running time is:%f\n", double (End-begin)/clocks _PER_SEC), clock () calculates the CPU execution time, note that cpu! If there are multiple cores in parallel, the final result is the sum of the computation time on each CPU! To be accurate to milliseconds, you can double (end-begin) *1000/clocks_per_sec Generally speaking, time is very good only if you want to be accurate to seconds.
    1. #include <</span>stdio.h>
    2. #include <</span>time.h>
    3. int main () {
    4. time_t T_start, T_end;
    5. T_start = time (NULL);
    6. Sleep (3000);
    7. T_end = time (NULL);
    8. printf ("Time:%.0f s\n", Difftime (T_end,t_start));
    9. return 0;
    10. }

If you want the program to hibernate for 3 seconds, Windows uses sleep (3), the unit of the parameter for Windows sleep interface is milliseconds, and the parameters of the sleep interface for Linux are in seconds.

If you need to be accurate to milliseconds, the above program will not work, if the Java to achieve this requirement is very simple, the code is as follows:

Download: Time.java
  1. public class Time {
  2. public static void Main (string[] args) {
  3. try {
  4. Long startTime = System.currenttimemillis ();
  5. Thread.Sleep (3000);
  6. Long endTime = System.currenttimemillis ();
  7. System.out.println ("Time:" + (Endtime-starttime) + "MS");
  8. } catch (Interruptedexception e) {
  9. E.printstacktrace ();
  10. }
  11. }
  12. }

Through Google to find some information, found that there is no standard in C language interface can be accurate to milliseconds, will be called to the operating system-related APIs, the following will be described in Linux and Windows system under the various implementation methods, we hope to be helpful.

Linux Systems

Using the Gettimeofday interface:

Download: gettimeofday.c
    1. #include <</span>stdio.h>
    2. #include <</span>sys/time.h>
    3. int main () {
    4. struct timeval start, end;
    5. Gettimeofday (&start, NULL);
    6. Sleep (3);
    7. Gettimeofday (&end, NULL);
    8. int timeuse = 1000000 * (end.tv_sec-start.tv_sec) + end.tv_usec-start.tv_usec;
    9. printf ("Time:%d us\n", timeuse);
    10. return 0;
    11. }

Gettimeofday can get microseconds, which is more accurate than milliseconds.

Using the Ftime interface:

Download: ftime.c
    1. #include <</span>stdio.h>
    2. #include <</span>sys/timeb.h>
    3. Long Long GetSystemTime () {
    4. struct TIMEB t;
    5. Ftime (&t);
    6. return * t.time + t.millitm;
    7. }
    8. int main () {
    9. Long long start=getsystemtime ();
    10. Sleep (3);
    11. Long long end=getsystemtime ();
    12. printf ("Time:%lld ms\n", End-start);
    13. return 0;
    14. }
Windows system

Using the GetTickCount interface:

Download: gettickcount.c
    1. #include <</span>windows.h>
    2. #include <</span>stdio.h>
    3. int main () {
    4. DWORD start, stop;
    5. Start = GetTickCount ();
    6. Sleep (3000);
    7. Stop = GetTickCount ();
    8. printf ("Time:%lld ms\n", Stop-start);
    9. return 0;
    10. }

Some compilers in the Windows system use printf to output 64-bit integer parameters using%i64d, such as VCs.

Using the Queryperformancex interface:

Download: queryperformance.c
  1. #include <</span>windows.h>
  2. #include <</span>stdio.h>
  3. int main () {
  4. Large_integer Li;
  5. Longlong start, end, freq;
  6. QueryPerformanceFrequency (&li);
  7. Freq = li. QuadPart;
  8. QueryPerformanceCounter (&li);
  9. Start = li. QuadPart;
  10. Sleep (3000);
  11. QueryPerformanceCounter (&li);
  12. End = Li. QuadPart;
  13. int usetime = (int) ((end-start) * 1000/freq);
  14. printf ("Time:%d ms\n", usetime);
  15. return 0;
  16. }

Using the GetSystemTime interface:

Download: getsystemtime.c
    1. #include <</span>windows.h>
    2. #include <</span>stdio.h>
    3. int main () {
    4. SYSTEMTIME currenttime;
    5. GetSystemTime (&currenttime);
    6. printf ("Time:%u/%u/%u%u:%u:%u:%u%d\n",
    7. Currenttime.wyear,currenttime.wmonth,currenttime.wday,
    8. Currenttime.whour,currenttime.wminute,currenttime.wsecond,
    9. Currenttime.wmilliseconds,currenttime.wdayofweek);
    10. return 0;
    11. }

This method does not give the calculation time difference implementation, only gives how to use the GetSystemTime call to get the current times, the calculation of the difference is relatively simple, based on the year, month, day, hour, seconds and milliseconds to calculate an integer, and then subtract two integers.

C + + Computational program run time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.