This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

In the process of SCM project development, there are strict requirements on the time of code execution in special applications, so how to accurately test the speed of each function is how fast? The test method is usually through the oscilloscope.

For example, to test the use of binary search, finding a number in the array takes several times. The test method is: every time the search algorithm is run, let the LED pin level be reversed once, and then use an oscilloscope to observe the LED pin waveform, and the execution time of this algorithm can probably be tested.

Test code is as follows:

#include "iostm8s103F3.h"
#include "led.h"
#include "find.h"

void sysclkinit( void )
{
    CLK_SWR = 0xe1;       //HSI 16MHz CPU clock frequency of the primary clock source
    CLK_CKDIVR = 0x00;    // The CPU clock is 0 and the system clock is 0
}

void main( void )
{
    sysclkinit();		          // Initialize the clock
    __asm( "sim" );                       // Disable interrupt
    led_init();
    __asm( "rim" );                       // Enable interrupt
    while( 1) { LED = ! LED; val = binary_search( (int* )volref, 500.0.sizeof( volref ) / sizeof( volref[0])); }}Copy the code

Binary_search is a binary search function that searches for the number 500 in the array of 100 data. The function is executed once for each LED turn. Then through the oscilloscope to observe the LED waveform.

You can see that the LED pins have a high and low level time of 164us, which means that the binary search method used to find the number 500 is 164us. The speed of the LED pin itself is less than 0.1us, so the time used by the LED pin itself to flip is negligible here.

In this way, the time taken by the specific execution of the function to be tested can be observed through the oscilloscope through the flip of the LED pin in the MCU.

This test is positive and accurate, but also more troublesome, every time the test, but also must pass the hardware circuit to test, then can not use the hardware circuit, directly through the software test? There is, of course, a way to test the execution time of each function in Windows through software emulation in the C compiler.

The compiler used here is dev-C ++, which can directly run the C function written in the single chip microcomputer, and then provide a few time functions through the system, you can accurately test the function running time.

The system provides functions that can be tested are as follows

  1. Clock () function

The clock() function returns an approximation of processor time used by the program.

The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide byCLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the functionreturns the value (clock_t) -1.

In simple terms, it is the amount of time that the program consumes CPU from startup to the function call. This function returns the number of CPU clock ticks between “start the program process” and “call the clock() function in the program.”

The function prototype is as follows:

The return value is of type clock_t, which is actually a long integer.

When called, this function returns a count of the CPU’s current time. To test the time, call the clock() function once before the function to store the current time count. Then call the function to be tested. After the test function is finished, call the clock() function once, read the current clock count, and subtract the previous clock count from this to calculate the time it took the function to execute. The default unit of time is ms.

2. Function GetTickCount ()

GetTickCount returns (Retrieve) the number of milliseconds elapsed (Elapsed) from boot from the operating system. It returns a DWORD, which is also a long integer.

GetTickCount() is called once before the function is executed, and then the function is executed. After the function is executed, GetTickCount() is called once again, and then the difference between the two measurements is calculated. This unit is also Ms.

It feels that MS is very short in Windows system, but ms time is relatively long for SCM, and the execution time of many functions is far less than 1ms. If the above two methods are used to test, the execution time of many functions can not be tested. Then you need to use a more accurate test function.

  1. The QueryPerformanceCounter () function

Calling this function returns the frequency of the high-precision counter supported by the hardware. Note that this returns the system timer value, not the time value. The above two functions return the system time value, while this function returns the system counter value, which is much more accurate than the time value, most of which can reach us level accuracy.

This function first before use to invoke QueryPerformanceFrequency () function for counting the frequency of the system, and also is the 1 s clock system counted how many times.

You then use QueryPerformanceCounter() to get the counter values before and after the program runs, and divide the difference between the two counter values by the frequency of the count to calculate the time taken for the function to execute.

Let’s start testing these three functions using Dev C++ software.

Write test code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

// Function to be tested
void fun(void)
{
	sleep(1);
}
typedef union _LARGE_INTEGER
{
	struct
	{
		long LowPart ;// A 4-byte integer
		long  HighPart;// A 4-byte integer
	};
	long long QuadPart;// The value is an 8-byte integer

} LARGE_INTEGER;

int main(int argc, char *argv[])
{
	int i = 0,val = 0;
	clock_t startTime,endTime;
	int time = 0;
	
	1 / / method
	startTime = clock();			// Start the timer
	fun();					// Call the function
	endTime = clock();			// The timer is over
	printf("1: program run time: %d ms\r\n\r\n\r\n",(endTime - startTime));

	2 / / method
	startTime = GetTickCount();		// Start the timer
	fun();					// Call the function
	endTime = GetTickCount();		// The timer is over
	printf("2: program run time: %d ms\r\n\r\n\r\n",(endTime - startTime));

	3 / / method
	LARGE_INTEGER secondcount= {0};
	LARGE_INTEGER startcount= {0};
	LARGE_INTEGER stopcount= {0};

	QueryPerformanceFrequency(&secondcount);     // Obtain the number of CPU Performance Tick units per second
	printf("3: System count frequency: %d \r\n",secondcount.QuadPart);

	QueryPerformanceCounter(&startcount);		// Start the timer
	fun();						// Call the function
	QueryPerformanceCounter(&stopcount);		// The timer is over
	
	time=( ((stopcount.QuadPart - startcount.QuadPart)*1000*1000)/secondcount.QuadPart);
	printf("Program run time: %d us\r\n\r\n",time);

	system("pause");
	return 0;
}

Copy the code

The result is as follows

The test function here is actually sleep(1); That is, sleep the clock for 1s, and then test the function execution time in each of the three ways.

The code execution time was 1000ms using the clock() function, 998ms using the GetTickCount() function, and 999630us or 999.63ms using the QueryPerformanceCounter() function. Through QueryPerformanceFrequency read () function to the system 1 s clock count number for 2630703 times.

Using the system’s built-in time function, you can directly test the execution time of each function, so that you can directly compare the performance of different functions without using hardware circuits.