Timing and delay function __jquery in c\c++

Source: Internet
Author: User

Two timing functions clock () and time () are available in the C\c++ standard library. Its usage is as follows:
(1) clock () function usage

void Timeconsume ()
{
    double start,stop,durationtime;
    start = Clock ();
    /*

    ... Code fragment

    *
    /stop = Clock ();

    Durationtime = ((double) (Stop-start))/clk_tck;
    cout << "program time Consuming:" << durationtime << "s" << Endl;
}

The clock () function returns a value type of clock_t. The function evaluates to a unit of internal processor time, so you must divide it by the clock frequency to get the time in seconds. This method is very accurate in measuring the machine. There are differences between Windows and Linux in this context.
In the Linux environment, the processor internal time frequency is: clocks_per_sec.

(2) time () function usage

void Timeconsume ()
{
    double start,stop,durationtime;
    Start = time (NULL);
    /*

    ... Code fragment

    *
    /stop = time (NULL);

    Durationtime = (double) difftime (stop, start);
    cout << "program time Consuming:" << durationtime << "s" << Endl;
}

The time () function returns a value type of time_t, so you also need to convert it to a double type before the output. Unlike clock, the time () function has a parameter that describes where the information is stored. Because you do not want to save this time, set the parameter to NULL. But this timing method is not as accurate as the clock () function, but it does not require information about the clock frequency.

(3) Delay function

In a standard library, you can use the sleep () function to delay the unit in milliseconds, and if you want to delay 5 seconds, the method is as follows:

Sleep (5*1000);

The _sleep () function can also be used in MFC to implement the delay function. In a Windows environment, the sleep () function is capitalized, while the sleep () function is lowercase in the Linux system. The sleep () function in a Linux system is in seconds instead of milliseconds, while the sleep () function in the Windows environment is in milliseconds. This is the difference between the two.

The timer delay function code in Windows environment is as follows:

#include <iostream>
#include <Windows.h>

using namespace std;
int main ()
{
    Double start, stop, durationtime;
    start = Clock ();

    Sleep (5 * 1000);    Program Delay 5s
    stop = Clock ();
    Durationtime = ((double) (Stop-start))/clk_tck;
    cout << "Total time consuming:" << durationtime << endl << Endl;
    return 0;
}

The results of the run are as shown in the figure:

The code in the Linux environment is as follows:

#include <iostream>
#include <unistd.h>

using namespace std;

int main ()
{
    double starttime,stoptime,durationtimetime;

    Starttime=time (NULL);

    Sleep (5);

    Stoptime=time (NULL);

    Durationtimetime = (double) difftime (stoptime,starttime);

    cout << "Time-consuming (time):" << durationtimetime << "s" << Endl;


    return 0;
}

The results of the run are as shown in the figure:

Interested partners in the Linux environment to try the clock () function, dumbfounded ...

Related Article

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.