Performance test of X& operator=(X)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* TestOpAssign.cpp | |
* | |
* Created by Martin on 28 September 2012. | |
* Copyright 2012 Universiteit Leiden. All rights reserved. | |
* | |
* Distributed under the Boost Software License, Version 1.0. (See accompanying | |
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
*/ | |
// g++ -O2 -o TestOpAssign.exe TestOpAssign.cpp && TestOpAssign | |
// cl -nologo -W3 -EHsc -O2 TestOpAssign.cpp && TestOpAssign | |
#include <iomanip> | |
#include <iostream> | |
#include <string> | |
#include <time.h> | |
#include <cstdlib> // for std::strtol() | |
template < typename T > | |
class Conventional | |
{ | |
public: | |
typedef T value_type; | |
typedef Conventional class_type; | |
Conventional( value_type const x = value_type() ) : v( x ) {} | |
Conventional( class_type const & other ) : v( other.v ) {} | |
class_type & operator=( class_type const & other ) | |
{ | |
v = other.v; | |
return *this; | |
} | |
private: | |
T v; | |
}; | |
template < typename T > | |
class CopySwap | |
{ | |
public: | |
typedef T value_type; | |
typedef CopySwap<T> class_type; | |
CopySwap( value_type const x = value_type() ) : v( x ) {} | |
CopySwap( class_type const & other ) : v( other.v ) {} | |
class_type & operator=( class_type other ) | |
{ | |
swap( *this, other ); | |
return *this; | |
} | |
friend void swap( class_type & first, class_type & second ) | |
{ | |
using std::swap; | |
swap( first.v, second.v ); | |
} | |
private: | |
T v; | |
}; | |
#define ASSIGN( x, y ) \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
x = y; \ | |
template < typename X > | |
int do_work( X & a, X const & b ) | |
{ | |
const int mega = 1000000; | |
const int count_ms = 2 * CLOCKS_PER_SEC / 10; | |
const clock_t te = clock() + count_ms; | |
int i = 0; | |
while ( clock() < te ) | |
{ | |
for( int j = 0; j < mega; ++j ) | |
{ | |
ASSIGN( a, b ); | |
} | |
++i; | |
} | |
return i; | |
} | |
inline const std::string to_string( const long x, const int base = 10 ) | |
{ | |
char buf[65]; | |
return _ltoa( x, buf, base ); | |
} | |
std::string compiler_name() | |
{ | |
#if defined( _MSC_VER ) | |
return "MS VC " + to_string(_MSC_VER / 100 - 6) + "." + to_string(_MSC_VER % 100) ; | |
#elif defined( __GNUC__ ) | |
return "GCC " + to_string(__GNUC__) + "." + to_string(__GNUC_MINOR__) + "." + to_string(__GNUC_PATCHLEVEL__) ; | |
// return std::string("GCC") + " " + to_string(__GNUC__) + "." + to_string(__GNUC_MINOR__) + "." + to_string(__GNUC_PATCHLEVEL__) ; | |
#endif | |
return "Unknown"; | |
} | |
int main() | |
{ | |
std::cout << | |
"\nPerformance test of X& operator=(X)." << | |
"\n" | |
"\nCompiler: " << compiler_name(); | |
int k1 = 0; | |
const clock_t t0 = clock(); | |
{ | |
Conventional<double> a; | |
Conventional<double> b; | |
k1 = do_work( a, b ); | |
} | |
int k2 = 0; | |
const clock_t t1 = clock(); | |
{ | |
CopySwap<double> a; | |
CopySwap<double> b; | |
k2 = do_work( a, b ); | |
} | |
const clock_t t2 = clock(); | |
const double cps = CLOCKS_PER_SEC; | |
std::cout << std::setprecision(2) << | |
"\tConventional: " << 1e2 * ((t1-t0)/cps)/k1 << " ns" << | |
"\tCopy-swap: " << 1e2 * ((t2-t1)/cps)/k2 << " ns" << | |
std::endl; | |
return 0; // VC6 | |
} | |
/* | |
* end of file | |
*/ |
Compiler: GCC 4.5.2 Conventional: 3.5 ns Copy-swap: 3.3 ns
Compiler: MS VC 6.0 Conventional: 7.5e-006 ns Copy-swap: 6.7 ns
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiler: MS VC 6.0 Conventional: 7.5e-006 ns Copy-swap: 6.7 ns
Compiler: GCC 4.5.2 Conventional: 3.5 ns Copy-swap: 3.3 ns