/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/test/function_benchmark/benchmark_impl.h"
-#include "folly/test/function_benchmark/test_functions.h"
+#include <folly/test/function_benchmark/benchmark_impl.h>
+#include <folly/test/function_benchmark/test_functions.h>
-#include "folly/Benchmark.h"
-#include "folly/ScopeGuard.h"
-#include <gflags/gflags.h>
#include <glog/logging.h>
+#include <folly/Benchmark.h>
+#include <folly/ScopeGuard.h>
+#include <folly/portability/GFlags.h>
+
using folly::ScopeGuard;
using folly::makeGuard;
// Directly invoking a function
BENCHMARK(fn_invoke, iters) {
- for (int n = 0; n < iters; ++n) {
+ for (size_t n = 0; n < iters; ++n) {
doNothing();
}
}
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;
// 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();
}
// 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<void()> 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<void()> 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)();
}
// 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<void()> fn = std::bind(&TestClass::doNothing, &tc);
fn();
}
// 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();
}
// Using ScopeGuard to invoke a std::function
BENCHMARK(scope_guard_std_function, iters) {
std::function<void()> fn(doNothing);
- for (int n = 0; n < iters; ++n) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(fn);
}
}
// 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) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(std::function<void()>(doNothing));
}
}
+// 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) {
+ ScopeGuard g = makeGuard(folly::Function<void()>(doNothing));
+ }
+}
+
// Using ScopeGuard to invoke a function pointer
BENCHMARK(scope_guard_fn_ptr, iters) {
- for (int n = 0; n < iters; ++n) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(doNothing);
}
}
// Using ScopeGuard to invoke a lambda that does nothing
BENCHMARK(scope_guard_lambda_noop, iters) {
- for (int n = 0; n < iters; ++n) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([] {});
}
}
// Using ScopeGuard to invoke a lambda that invokes a function
BENCHMARK(scope_guard_lambda_function, iters) {
- for (int n = 0; n < iters; ++n) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([] { doNothing(); });
}
}
// 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) {
+ for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([&] {
// Increment count if n is odd. Without this conditional check
// (i.e., if we just increment count each time through the loop),
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) {
}
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) {
}
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<void()> f(a);
+ invoke(std::move(f));
+ }
+}
+
+BENCHMARK(Function_create_move_invoke, iters) {
+ LargeClass a;
+ for (size_t i = 0; i < iters; ++i) {
+ folly::Function<void()> 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<void()> 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<void()> 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();
}