Time Interval Acquisition Method for Performance Testing

Source: Internet
Author: User

A problem during performance testing is the time interval. Many time functions can obtain the time to obtain the time interval. I will not repeat many articles in this regard. Here I only provide some basic information to facilitate my own performance testing:

(1) c language time functions clock () and clock_t:

The C language time function clock () in time. h can get the current time. Note that this function is common in Windows and Linux, but the unit of return value is different. One is millisecond and the other is microsecond.

The Unit also shows the precision of the time obtained by this method. In general, the precision is within milliseconds. Therefore, this function is sufficient for testing the performance during the coarse interval.

In Linux, clock () obtains the CPU time, not the wall-clock time. Therefore, if you use functions such as sleep, it will not be included. However, on Windows, clock () obtains the time of sleep () and other functions. Therefore, pay attention to this when using clock, what we get in Linux is probably not a correct elapsed time.

(2) Use the CPU to obtain the high-precision time rdtsc register

Rdtsc is a 64-bit register, which is used to record the clock cycle of the CPU starting from the computer, it can be seen that the high accuracy is at the CPU frequency level.

For more information, see http://blog.sina.com.cn/s/blog_5d9051c00100jcsn.html.

Because it is a register, you only need to use the Assembly to read the value of the Register to get the time. There are some different ways to write the Assembly. For details, refer to the online writing method, in addition, Windows and Linux may have some differences. It should be noted that, even in Linux or windows, the 64-bit and 32-bit compiling methods are different. For details, refer to: Compile? Treeid = 624 (the time interval may be negative if used incorrectly)

PS: in Linux, the-M32 and-M64 options indicate that 32-bit and 64-bit programs are generated, and the _ i386 _ Macro will be defined using-M32, use-M64 to use _ x86_64 _ macro.

For Windows, you can also use the rdtsc register, but when a 64-bit program is generated on windows, I do not know how to get the correct value, refer to http://sunxiunan.com /? P = 983 is 64-bit, but it can only be used on itanium CPU. According to msdn instructions (http://msdn.microsoft.com/en-us/library/ee417693.aspx), rdtsc registers are not recommended and more precise windows is recommended
The queryperformancecounter and queryperformancefrequency of the API.

(3) Use of queryperformancecounter and queryperformancefrequency on Windows:

Reference: http://www.oschina.net/code/snippet_197161_6789

(4) timing. h and test:

The following is the code that can run on Linux and Windows and can be used to calculate the program running time. The same performance test should be competent.

// Timing.h#ifndef TIMING_H#define TIMING_H#include <time.h>#include <stdio.h>#ifdef WIN#include <windows.h>#define timing_t doublestatic _LARGE_INTEGER time_start, time_over;static double dqFreq;static inline void startTiming(){_LARGE_INTEGER f;QueryPerformanceFrequency(&f);dqFreq=(double)f.QuadPart;QueryPerformanceCounter(&time_start);}// unit: msstatic inline timing_t stopTiming(){    QueryPerformanceCounter(&time_over);    return ((double)(time_over.QuadPart-time_start.QuadPart)/dqFreq*1000);}static inline timing_t stopWithPrintTiming(){    timing_t timing;QueryPerformanceCounter(&time_over);    timing = ((double)(time_over.QuadPart-time_start.QuadPart)/dqFreq*1000);    printf("----------Elapsed Timing(ms) : %.3lf\n", timing);    printf("----------------------------------------\n");    return timing;}#else#include <unistd.h>typedef unsigned long long int64;#define timing_t int64#if defined(__i386__)inline int64 GetCycleCount() {    int64 result;    __asm__ __volatile__ ("rdtsc" : "=A" (result));    return result;}#elif defined(__x86_64__)inline int64 GetCycleCount() {    int64 hi, lo;    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));    return ( (int64)lo)|( ((int64)hi)<<32 );}#endifstatic int64 ticks_start, ticks_end;static inline void startTiming(){ticks_start = GetCycleCount();}// unit: cyclesstatic inline int64 stopTiming(){    ticks_end = GetCycleCount();    return (ticks_end - ticks_start);}static inline int64 stopWithPrintTiming(){    int64 timing;    ticks_end = GetCycleCount();    timing = (ticks_end - ticks_start);    printf("----------Elapsed Timing(Cycles) : %llu\n", timing);    printf("----------------------------------------\n");    return timing;}#endif// unit: msstatic inline void wait(int ms){#ifdef WIN32    Sleep(ms);#else    usleep(ms*1000);#endif}#endif

// foo.cpp#include "timing.h"int main(){timing_t ticks_start, ticks_end;startTiming();    wait(3000);// 3 seconds    timing_t timing = stopWithPrintTiming();    return 1;}

Compile on Windows: CL Foo. CPP/dwin and start the 64-bit vs console for testing respectively.

Compile on Linux: GCC Foo. cpp-M32 and GCC Foo. cpp-M64 for testing.

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.