Recently, a simulated annealing algorithm was written in C + +, and the performance of the algorithm was tested by camel function, and the value of camel function was calculated very many times in program operation due to the characteristic of simulated annealing algorithm.
First introduce the Camel function:
The expression of the function has an X four power, multiple x, y squared terms, the first is to use the POW () function to calculate, and then directly with the multiplication of the expression to calculate, do not know which will be faster, this for the SA algorithm performance is quite critical, so did a simple test, the specific look at the code:
FUNC1: all with multiplication;
FUNC2: Four Power with POW (), and the rest by multiplication;
FUNC3: All in the POW () function to calculate;
#include <iostream>#include<cmath>#include<ctime>#include<vector>using namespacestd;//Func1: all with multiplicationDoubleFUNC1 (Constvector<Double> &State ) { Doublex = state[0]; Doubley = state[1]; DoubleF = (4-2.1*x*x + x*x*x*x/3.0) *x*x + X*y + (-4+4.0* y*y) *y*y; returnF; }//FUNC2: Four Power with POW (), rest with multiplicationDoubleFUNC2 (Constvector<Double> &State ) { Doublex = state[0]; Doubley = state[1]; DoubleF; F= (4-2.1*x*x + POW (x,4) /3.0) *x*x + X*y + (-4+4.0* y*y) *y*y; returnF;}//Func3: All with the POW () function to calculateDoubleFUNC3 (Constvector<Double> &State ) { Doublex = state[0]; Doubley = state[1]; DoubleF; F= (4-2.1*pow (x,2) + POW (x,4) /3.0) *pow (x,2) + X*y + (-4+4.0* POW (Y,2)) *pow (Y,2); returnF;}intmain () {vector<Double> Initialstate (2); initialstate[0] =0.0898; initialstate[1] = -0.7126; clock_t start, end; //test of Func1 inti =10000000; Start=clock (); while(i--) {Func1 (initialstate); } End=clock (); cout<<"Func1 Cost:"<< (Double) (End-start)/clocks_per_sec <<Endl; //test of Func2 intj =10000000; Start=clock (); while(j--) {FUNC2 (initialstate); } End=clock (); cout<<"Func2 Cost:"<< (Double) (End-start)/clocks_per_sec <<Endl; //test of Func3 intK =10000000; Start=clock (); while(k--) {Func3 (initialstate); } End=clock (); cout<<"Func3 Cost:"<< (Double) (End-start)/clocks_per_sec <<Endl; GetChar (); return 0;}
The results are as follows:
As you can see, after running 10 million times, the calculation speed gap is reflected, the fastest use of multiplication, about only a third function of 35%
Pow () function and performance comparison of direct multiplication--take camel function as an example