Line data Source code
1 :
2 : /* Copyright (c) 2014, Stefan.Eilemann@epfl.ch
3 : *
4 : * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #define BOOST_TEST_MODULE Future
21 :
22 : #include <lunchbox/clock.h>
23 : #include <lunchbox/readyFuture.h>
24 : #include <boost/test/unit_test.hpp>
25 :
26 3 : BOOST_AUTO_TEST_CASE( readyFutures )
27 : {
28 1 : lunchbox::f_bool_t ok = lunchbox::makeTrueFuture();
29 1 : BOOST_CHECK( ok == true );
30 1 : BOOST_CHECK( ok.isReady( ));
31 :
32 2 : lunchbox::f_bool_t nok = lunchbox::makeFalseFuture();
33 1 : BOOST_CHECK( nok == false );
34 2 : BOOST_CHECK( nok.isReady( ));
35 1 : }
36 :
37 2000000 : static lunchbox::f_bool_t futureFunction()
38 : {
39 2000000 : return lunchbox::makeTrueFuture();;
40 : }
41 :
42 1000000 : static bool presentFunction()
43 : {
44 1000000 : return true;
45 : }
46 :
47 3 : BOOST_AUTO_TEST_CASE( perfFuture )
48 : {
49 : static const size_t nLoops = 1000000;
50 :
51 1 : lunchbox::Clock clock;
52 1000001 : for( size_t i = 0; i < nLoops; ++i )
53 1000000 : futureFunction();
54 1 : const float futureSync = clock.resetTimef();
55 :
56 2 : std::vector< lunchbox::f_bool_t > futures;
57 1 : futures.reserve( nLoops );
58 1000001 : for( size_t i = 0; i < nLoops; ++i )
59 1000000 : futures.push_back( futureFunction( ));
60 1 : futures.clear();
61 1 : const float futureASync = clock.resetTimef();
62 :
63 1000001 : for( size_t i = 0; i < nLoops; ++i )
64 1000000 : presentFunction();
65 1 : const float present = clock.resetTimef();
66 :
67 1 : std::cout << nLoops / futureSync / 1000.f << " sync futures, "
68 2 : << nLoops / futureASync / 1000.f << " async futures, "
69 3 : << nLoops / present / 1000.f << " normal calls/us" << std::endl;
70 4 : }
|