Obtains the "alarm time", "user time", and "system time" of program execution"

Source: Internet
Author: User

Simple programs can be implemented through commands

[Plain]
# Time./test
 
Real 0m2. 033 s
User 0m2. 032 s
Sys 0m0. 000 s

# Time./test

Real 0m2. 033 s
User 0m2. 032 s
Sys 0m0. 000 s

If the time command is not convenient, you can use system functions.

[Cpp]
# Include <sys/times. h>
 
// The user obtains the user time and system time.
Static struct tms tms_start;
Static struct tms tms_end;
 
// Used to get the clock-up time
Static clock_t c_start;
Static clock_t c_end;
 
Void debug_times_start ()
{
Memset (& tms_start, 0, sizeof (struct tms ));
Memset (& tms_end, 0, sizeof (struct tms ));
 
C_start = times (& tms_start );
}
 
Void debug_times_end ()
{
C_end = times (& tms_end );
 
Printf ("clock time: % ld, user/system cpu time: % ld/% ld \ r \ n ",
C_end-c_start,
Tms_end.tms_utime-tms_start.tms_utime,
Tms_end.tms_stime-tms_start.tms_stime );
}

# Include <sys/times. h>

// The user obtains the user time and system time.
Static struct tms tms_start;
Static struct tms tms_end;

// Used to get the clock-up time
Static clock_t c_start;
Static clock_t c_end;

Void debug_times_start ()
{
Memset (& tms_start, 0, sizeof (struct tms ));
Memset (& tms_end, 0, sizeof (struct tms ));

C_start = times (& tms_start );
}

Void debug_times_end ()
{
C_end = times (& tms_end );

Printf ("clock time: % ld, user/system cpu time: % ld/% ld \ r \ n ",
C_end-c_start,
Tms_end.tms_utime-tms_start.tms_utime,
Tms_end.tms_stime-tms_start.tms_stime );
}


 

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.