The performance of floating point calculations, compared to integers

xiaoxiao2021-03-06  14

Test Results:

Test Std :: Vector ( : 234) (-: 266) (/: 1687) (/: 219) (-: 234) (*: 282) (/: 1656) ( : 235) (-: 235) (/: 1625) ( : 235) (-: 218) (*: 297) (/: 1594)

Test Std :: Vector (1000): ( : 313) (/: 1625) ( : 218) (-: 235) (*: 265) (/: 1704) ( : 219) (-: 219) (/: 1594) ( : 234) (-: 234) (*: 282) (/: 2109)

Test Std :: Vector (1000): ( : 265) (/: 2328) ( : 250) (-: 250) (*: 250) (/: 2281) ( : 250) (-: 250) (/: 2609) ( : 250) (-: 250) (*: 250) (/: 250) (/: 2781)

Test Std :: Vector (1000): ( : 344) (/: 242) (/: 242) ( : 344) (-: 328) (*: 578) (/: 2422) ( : 360) (-: 459) (/: 2422) ( : 297) (-: 328) (*: 313) (/: 2312)

in conclusion:

INT, long, float, double, -, * calculation performance is the same, the division is slow, more occupied, to avoid division.

That is: the calculation time of adding, minus, and multiplication is independent of the data type, so it is possible to use a more accurate FLOAT, DOUBLE type, which does not affect the program speed.

The following is a test code (please generate a console type with VC7, save the following code as a file join item, Release mode compilation):

#include #include #include #include #include #include #include // for boost :: bind # incrude // for boost :: lambda # include

Template Void Test (T & Vec, To & Out) {OUT << "Test Std :: Vector <<< TypeId (T :: Value_Type) .Name () <<"> ("<< Vec.size () << "): / t"; std :: vector MyVECTEST (4), mydst (vec.size ()); std :: generate (MyVECTEST.BEGIN (), MyVECTEST .end (), Boost :: Bind (std :: limited> (), Boost :: Bind (& rand), (T :: Value_Type) 9876))); std :: transform (MyVECTEST.BEGIN ), myvectest.end (), myvectest.begin (), boost :: bind (std :: plus (), 3, _1)); OUT << ("; std :: for_each MyVECTEST.BEGIN (), MyVECTEST.END (), OUT << Boost :: Lambda :: _ 1 << '/ t'); OUT << ")"; OUT << '/ n'; for (int TestIndex = 0; testindex

DWStartTime = clock (); for (int NTIMES = 0; NTIMES <100000; NTIMES) // calculated for the number of times for calculation for (INT i = 0, nsize = vec.size (); i );} int main (int Argc, char * argv []) {long lsize = 1000; std :: vector myvecint (lsize); std :: vector MyVeclong (LSIZ e); std :: vector myVecfloat (lsize); std :: vector myvecdouble; lsize);

// Generate test data std :: generate (myvecint.begin (), myvecint.ere (); std :: generate (myveclong.begin (), myveclong.erate (); std :: generate MYVECFLOAT.EGIN (), MyVecfloat.end (), Boost :: Bind (std :: divide :: bind)); std :: transform (MyVecfloat.begin (), MyVecfloat.end (), MyVecdouble.begin (), Boost :: Bind (std :: multiplies (), 3, _1));

// Show before n = 6; std :: for_each (myvecint.begin (), myvecint.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t' ); std :: cout << "/ n / n"; std :: for_each (MyVeclong.begin (), myveclong.begin () n, std :: cout << boost :: lambda :: _ 1 << ' / t '); std :: cout << "/ n / n"; std :: for_each (MyVecfloat.begin (), myVecfloat.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t'); std :: cout << "/ n / n"; std :: for_each (myVecdouble.begin (), myVecdouble.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t'); std :: cout << "/ n / n"; std :: ostream & of = std :: cout; // Out to screen // std :: OFSTREAM OF ("c : //testtime.txt ", std :: os_base :: out); // Output to File

Test (MyVeclong, Of); Test (MyVecfloat, of); Test (MyVecdouble, of); //of.close (); // Close file

// Show before n data std :: for_each (myvecint.begin (), myvecint.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t'); std :: Cout << "/ n / n"; std :: for_each (MyVeclong.begin (), myveclong.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t'); Std :: cout << "/ n"; std :: for_each (myVecfloat.begin (), myVecfloat.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t '); std :: cout << "/ n / n"; std :: for_each (myVecdouble.begin (), myvecdouble.begin () n, std :: cout << boost :: lambda :: _ 1 << '/ t'); std :: cout << "/ n / n";

转载请注明原文地址:https://www.9cbs.com/read-49106.html

New Post(0)