X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2Ffunction_benchmark%2Fmain.cpp;h=7b12aba339b5d88f1f29256af2b96c4666dee8ab;hb=717229ecf385ee10a91d6c2af859e8550df84531;hp=68c6a16593e4b94f92983eb46b784cedcceb760b;hpb=5c77fedbef46995a71ffa268c9fcaf49efddd01b;p=folly.git diff --git a/folly/test/function_benchmark/main.cpp b/folly/test/function_benchmark/main.cpp index 68c6a165..7b12aba3 100644 --- a/folly/test/function_benchmark/main.cpp +++ b/folly/test/function_benchmark/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,15 +14,15 @@ * limitations under the License. */ -#include "folly/test/function_benchmark/benchmark_impl.h" -#include "folly/test/function_benchmark/test_functions.h" +#include +#include -#include "folly/Benchmark.h" -#include "folly/ScopeGuard.h" -#include #include -using folly::ScopeGuard; +#include +#include +#include + using folly::makeGuard; // Declare the bm_max_iters flag from folly/Benchmark.cpp @@ -30,7 +30,7 @@ DECLARE_int32(bm_max_iters); // Directly invoking a function BENCHMARK(fn_invoke, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { doNothing(); } } @@ -45,6 +45,11 @@ BENCHMARK(std_function_invoke, iters) { BM_std_function_invoke_impl(iters, doNothing); } +// Invoking a function through a folly::Function object +BENCHMARK(Function_invoke, iters) { + BM_Function_invoke_impl(iters, doNothing); +} + // Invoking a member function through a member function pointer BENCHMARK(mem_fn_invoke, iters) { TestClass tc; @@ -97,7 +102,7 @@ BENCHMARK(virtual_fn_invoke, iters) { // Creating a function pointer and invoking it BENCHMARK(fn_ptr_create_invoke, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { void (*fn)() = doNothing; fn(); } @@ -105,16 +110,25 @@ BENCHMARK(fn_ptr_create_invoke, iters) { // Creating a std::function object from a function pointer, and invoking it BENCHMARK(std_function_create_invoke, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { std::function fn = doNothing; fn(); } } +// Creating a folly::Function object from a function pointer, and +// invoking it +BENCHMARK(Function_create_invoke, iters) { + for (size_t n = 0; n < iters; ++n) { + folly::Function fn = doNothing; + fn(); + } +} + // Creating a pointer-to-member and invoking it BENCHMARK(mem_fn_create_invoke, iters) { TestClass tc; - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { void (TestClass::*memfn)() = &TestClass::doNothing; (tc.*memfn)(); } @@ -124,7 +138,7 @@ BENCHMARK(mem_fn_create_invoke, iters) { // and invoking it BENCHMARK(std_bind_create_invoke, iters) { TestClass tc; - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { std::function fn = std::bind(&TestClass::doNothing, &tc); fn(); } @@ -133,7 +147,7 @@ BENCHMARK(std_bind_create_invoke, iters) { // Using std::bind directly to invoke a member function BENCHMARK(std_bind_direct_invoke, iters) { TestClass tc; - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { auto fn = std::bind(&TestClass::doNothing, &tc); fn(); } @@ -142,45 +156,59 @@ BENCHMARK(std_bind_direct_invoke, iters) { // Using ScopeGuard to invoke a std::function BENCHMARK(scope_guard_std_function, iters) { std::function fn(doNothing); - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard(fn); + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard(fn); + (void)g; } } // Using ScopeGuard to invoke a std::function, // but create the ScopeGuard with an rvalue to a std::function BENCHMARK(scope_guard_std_function_rvalue, iters) { - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard(std::function(doNothing)); + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard(std::function(doNothing)); + (void)g; + } +} + +// Using ScopeGuard to invoke a folly::Function, +// but create the ScopeGuard with an rvalue to a folly::Function +BENCHMARK(scope_guard_Function_rvalue, iters) { + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard(folly::Function(doNothing)); + (void)g; } } // Using ScopeGuard to invoke a function pointer BENCHMARK(scope_guard_fn_ptr, iters) { - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard(doNothing); + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard(doNothing); + (void)g; } } // Using ScopeGuard to invoke a lambda that does nothing BENCHMARK(scope_guard_lambda_noop, iters) { - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard([] {}); + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard([] {}); + (void)g; } } // Using ScopeGuard to invoke a lambda that invokes a function BENCHMARK(scope_guard_lambda_function, iters) { - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard([] { doNothing(); }); + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard([] { doNothing(); }); + (void)g; } } // Using ScopeGuard to invoke a lambda that modifies a local variable BENCHMARK(scope_guard_lambda_local_var, iters) { uint32_t count = 0; - for (int n = 0; n < iters; ++n) { - ScopeGuard g = makeGuard([&] { + for (size_t n = 0; n < iters; ++n) { + auto g = makeGuard([&] { // Increment count if n is odd. Without this conditional check // (i.e., if we just increment count each time through the loop), // gcc is smart enough to optimize the entire loop away, and just set @@ -189,6 +217,7 @@ BENCHMARK(scope_guard_lambda_local_var, iters) { ++count; } }); + (void)g; } // Check that the value of count is what we expect. @@ -200,7 +229,7 @@ BENCHMARK(scope_guard_lambda_local_var, iters) { BENCHMARK_DRAW_LINE() BENCHMARK(throw_exception, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { try { throwException(); } catch (const std::exception& ex) { @@ -209,7 +238,7 @@ BENCHMARK(throw_exception, iters) { } BENCHMARK(catch_no_exception, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { try { doNothing(); } catch (const std::exception& ex) { @@ -218,51 +247,85 @@ BENCHMARK(catch_no_exception, iters) { } BENCHMARK(return_exc_ptr, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { returnExceptionPtr(); } } BENCHMARK(exc_ptr_param_return, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { std::exception_ptr ex; exceptionPtrReturnParam(&ex); } } BENCHMARK(exc_ptr_param_return_null, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { exceptionPtrReturnParam(nullptr); } } BENCHMARK(return_string, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { returnString(); } } BENCHMARK(return_string_noexcept, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { returnStringNoExcept(); } } BENCHMARK(return_code, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { returnCode(false); } } BENCHMARK(return_code_noexcept, iters) { - for (int n = 0; n < iters; ++n) { + for (size_t n = 0; n < iters; ++n) { returnCodeNoExcept(false); } } +BENCHMARK_DRAW_LINE() + +BENCHMARK(std_function_create_move_invoke, iters) { + LargeClass a; + for (size_t i = 0; i < iters; ++i) { + std::function f(a); + invoke(std::move(f)); + } +} + +BENCHMARK(Function_create_move_invoke, iters) { + LargeClass a; + for (size_t i = 0; i < iters; ++i) { + folly::Function f(a); + invoke(std::move(f)); + } +} + +BENCHMARK(std_function_create_move_invoke_ref, iters) { + LargeClass a; + for (size_t i = 0; i < iters; ++i) { + std::function f(std::ref(a)); + invoke(std::move(f)); + } +} + +BENCHMARK(Function_create_move_invoke_ref, iters) { + LargeClass a; + for (size_t i = 0; i < iters; ++i) { + folly::Function f(std::ref(a)); + invoke(std::move(f)); + } +} + // main() int main(int argc, char** argv) { - google::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); folly::runBenchmarks(); }