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 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <functional>
#include <vector>
#include <numeric>
#include <iterator>
#include <ctime>
double fun( double a, double b, double c ) { return a*a*a - b*b + c ; }
constexpr std::size_t N = 50'000'000 ;
int a[N], b[N], c[N], result[N] ;
int main()
{
std::iota( std::begin(a), std::end(a), 1 ) ;
std::iota( std::begin(b), std::end(b), N+1 ) ;
std::iota( std::begin(c), std::end(c), N*2 + 1 ) ;
{
const auto start = std::clock() ;
const auto pfn = &fun ;
for( std::size_t i = 0 ; i < N ; ++i ) result[i] = pfn( a[i], b[i], c[i] ) ;
const auto end = std::clock() ;
std::cout << " raw pointer to function: " << (end-start) * 1000.0 / CLOCKS_PER_SEC << " milliseconds\n" ;
}
{
const auto start = std::clock() ;
const std::function< double(double,double,double) > fn = fun ;
for( std::size_t i = 0 ; i < N ; ++i ) result[i] = fn( a[i], b[i], c[i] ) ;
const auto end = std::clock() ;
std::cout << "wrapped pointer to function: " << (end-start) * 1000.0 / CLOCKS_PER_SEC << " milliseconds\n" ;
}
{
const auto start = std::clock() ;
const std::function< double(double,double,double) > fn = []( double a, double b, double c ) { return a*a*a - b*b + c ; };
for( std::size_t i = 0 ; i < N ; ++i ) result[i] = fn( a[i], b[i], c[i] ) ;
const auto end = std::clock() ;
std::cout << " wrapped closure: " << (end-start) * 1000.0 / CLOCKS_PER_SEC << " milliseconds\n" ;
}
}
|