1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <ctime>
#include <functional>
#include <algorithm>
#include <numeric>
int foo( int a ) ;
std::function< int(int) > wrapped_foo() ;
std::function< int(int) > wrapped_closure() ;
template < std::size_t N > int bar( const int (&srce)[N], int (&dest)[N], const std::function< int(int) >& fn )
{
const auto start = std::clock() ;
std::transform( srce, srce+N, dest, fn ) ;
return ( std::clock() - start ) * 1000.0 / CLOCKS_PER_SEC ;
}
int main()
{
constexpr std::size_t N = 1024*1024*32 ;
static int srce[N] {};
static int dest[N] {};
std::iota( srce, srce+N, 0 ) ;
std::iota( dest, dest+N, 0 ) ; // to get equivalent cache locality for all cases
std::cout << " raw function pointer: " << bar( srce, dest, &foo ) << " millisecs\n" ;
std::cout << "wrapped function pointer: " << bar( srce, dest, wrapped_foo() ) << " millisecs\n" ;
std::cout << " wrapped closure (local): " << bar( srce, dest, []( int a ) { return a/2 + a%3 ; } ) << " millisecs\n" ;
std::cout << "wrapped closure (extern): " << bar( srce, dest, wrapped_closure() ) << " millisecs\n" ;
std::cout << "------------------------------------------------\n" ;
std::cout << " raw function pointer: " << bar( srce, dest, &foo ) << " millisecs\n" ;
std::cout << "wrapped function pointer: " << bar( srce, dest, wrapped_foo() ) << " millisecs\n" ;
std::cout << " wrapped closure (local): " << bar( srce, dest, []( int a ) { return a/2 + a%3 ; } ) << " millisecs\n" ;
std::cout << "wrapped closure (extern): " << bar( srce, dest, wrapped_closure() ) << " millisecs\n" ;
}
|