Method of measuring code run time (linux/windows)

Source: Internet
Author: User
Tags intel pentium

Code run Time measurement method under Windows platform:

One: Millisecond level GetTickCount ()

#include <windows.h>

Second: Seconds-level time ()

#include <time.h>

Three: Microsecond level QueryPerformanceCounter (&end)

#include <windows.h>

Time measurement method under Linux platform:

One: seconds C language Library function time ()

Second: microsecond C language library function Gettimeofday ()

itself time consumption of about 3MS

Three: nanosecond-level RDTSC instruction timestamp timer

Intel Pentium above CPU, there is a part called "Time Stamp", it is in the format of 64-bit unsigned shaping, accurately records the number of clock cycles that have elapsed since the CPU was power up. The timestamp counter is in the machine clock unit. 1: Cycle = 1 sec/main frequency (Hz). These CPUs can be read using the RDTSC (read time Stamp Counter) instruction, which loads the high 32 bits of the timestamp counter into the edx register, and the low 32 bits are deposited in the EAX register. The RDTSC directive is not directly supported by the GCC inline assembly and can be embedded with a macro definition:

#define RDTSC ". Byte 0x0f, 0x31\n\t"

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.