Performance comparison of POW () function and direct multiplication--taking camel function as an example

Source: Internet
Author: User

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

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.