【Learning】-C/C++ various calculation speed summary

The environment used in this article:
Master control: arm-a53
Compilation environment: g++

Development board:
insert image description here

Foreword:
This article is for learning only. Record the impact on time of different optimizations and different looping methods.

1. Array and pointer loop calculation

1. Array multiplication

long long sum = 0;
char mmm[num] = {
    
    0};
char ttt[num] = {
    
    0};

void test1()
{
    
    
  	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += mmm[i] * ttt[i];
	}

}

2. Pointer multiplication

char mm[num] = {
    
    0};
char tt[num] = {
    
    0};
char *m = mm;
char *t = tt;
void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += *m * *t;
		m++;
		t++;
	}

}

3. Multiplication within a pointer

char mmmm[num] = {
    
    0};
char tttt[num] = {
    
    0};
char *mmmmm = mmmm;
char *ttttt = tttt;
void test3()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += *mmmmm++ * *ttttt++;
	}
}

4. Main function

#include <stdio.h>
#include <cstring>
#include <time.h>              

#define  num  10000000


int main()
{
    
    
	clock_t start, end; 

	memset(mm,22,num);
	memset(tt,22,num);
	memset(mmm,22,num);
	memset(ttt,22,num);
	memset(mmmm,22,num);
	memset(tttt,22,num);  
	   
	start = clock();         
	test1();
	end = clock();          
	double seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);   
	printf("%lld  \n",sum);
	
	start = clock();          
	test2();
	end = clock();        
	seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);      	
	printf("%lld  \n",sum);
	
	
	start = clock();          
	test3();
	end = clock();        
	seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);   
	printf("%lld  \n",sum);
}

2. Different optimization time

1. Not optimized

insert image description here

2. -O1 does not optimize

insert image description here

3. -O2 does not optimize

insert image description here

4. -O3 does not optimize

insert image description here
The conclusion drawn is to do -O3 optimization, use arrays.

3. Calculation time of multiplication and division

Note: All calculations below must be multiples of 2^n

1. Multiplication time

1.1, * multiplication calculation

void test1()
{
    
    
  sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mmm[i] * tt[i]);
	}

}

1.2, left shift calculation

void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mm[i] << 6);
	}

}

1.3. Main function

#include <stdio.h>
#include <cstring>
#include <time.h>              

#define  num  10000000

long long sum = 0;
char mmm[num] = {
    
    0};
char tt[num] = {
    
    0};

void test1()
{
    
    
  sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mmm[i] * tt[i]);
	}

}

char mm[num] = {
    
    0};
void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mm[i] << 6);
	}

}

int main()
{
    
    
	clock_t start, end; 

	memset(mm,64,num);
	memset(mmm,64,num);
 	memset(tt,64,num);
	   
	start = clock();         
	test1();
	end = clock();          
	double seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);   
	printf("%lld \n",sum);

	start = clock();          
	test2();
	end = clock();        
	seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);      	
	printf("%lld \n",sum);
	
}

1.4. Comparison of multiplication calculation time under different optimization levels

insert image description here

2. Division time

3.1, direct division

void test1()
{
    
    
  sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mmm[i] / tt[i]);
	}

}

3.2, right removal method

void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mm[i] >> 6);
	}

}

3.3. Main function

#include <stdio.h>
#include <cstring>
#include <time.h>              

#define  num  10000000

long long sum = 0;
char mmm[num] = {
    
    0};
char tt[num] = {
    
    0};

void test1()
{
    
    
  sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mmm[i] / tt[i]);
	}

}

char mm[num] = {
    
    0};
void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mm[i] >> 6);
	}

}

int main()
{
    
    
	clock_t start, end; 

	memset(mm,64,num);
	memset(mmm,64,num);
 	memset(tt,64,num);
	   
	start = clock();         
	test1();
	end = clock();          
	double seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);   
	printf("%lld \n",sum);

	start = clock();          
	test2();
	end = clock();        
	seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);      	
	printf("%lld \n",sum);
	
}

3.4. Comparison of division calculation time under different optimization levels

insert image description here

3. Take the remainder

3.1. Take the remainder directly

void test1()
{
    
    
  	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += mmm[i] % tt[i];
	}

}

3.2, & remainder


void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += mm[i] & (tt[i] - 1);
	}

}

3.3. Main function

#include <stdio.h>
#include <cstring>
#include <time.h>              

#define  num  10000000

float sum = 0;
char mmm[num] = {
    
    0};
char tt[num] = {
    
    0};

void test1()
{
    
    
  sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mmm[i] % tt[i]);
	}

}

char mm[num] = {
    
    0};
void test2()
{
    
    
	sum = 0;
	for(int i=0;i<num;i++)
	{
    
    
		sum += (mm[i] & (tt[i] - 1));
	}

}

int main()
{
    
    
	clock_t start, end; 

	memset(mm,64,num);
	memset(mmm,64,num);
 	memset(tt,64,num);
	   
	start = clock();         
	test1();
	end = clock();          
	double seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);   
	printf("%f \n",sum);

	start = clock();          
	test2();
	end = clock();        
	seconds  =(double)(end - start)/CLOCKS_PER_SEC;
	printf("Use1 time is: %.10f\n", seconds*1000);      	
	printf("%f \n",sum);
	
}

3.4. Comparison of remainder calculation time under different optimization levels

insert image description here

4. Summary

In fact, only in the case of a large number of calculations, there will be a significant difference in time. Of course, this does not mean that it is useless. When used at the bottom layer, there are significant advantages in using displacement operations first.

Guess you like

Origin blog.csdn.net/qq_37280428/article/details/124831464