Comments (7)
Just out of interest: How does the compile time of both compare? Is there a notable difference?
from functionalplus.
With the following sample (not a notable test sample):
#include <vector>
#include <functional>
#include <utility>
#include <type_traits>
#include <iostream>
#include <chrono>
template <typename TimeT = std::chrono::milliseconds>
struct measure
{
template <typename F, typename... Args>
static typename TimeT::rep execution(F func, Args &&... args)
{
auto start = std::chrono::system_clock::now();
// Now call the function with all the parameters you need.
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast<TimeT>(std::chrono::system_clock::now() - start);
return duration.count();
}
};
#ifdef VARIADIC
template <typename U, typename... V>
auto min(const U &u, const V &... v)
{
using rettype = typename std::common_type_t<U, V...>;
rettype result = static_cast<rettype>(u);
(void)std::initializer_list<bool>{((v < result) ? (result = static_cast<rettype>(v), false) : false)...};
return result;
}
#else
// API search type: min_2 : (a, a) -> a
// Minimum of two values.
template <typename X>
const X &min_2(const X &a, const X &b)
{
return std::min(a, b);
}
// API search type: min_3 : (a, a, a) -> a
// Minimum of three values.
template <typename X>
const X &min_3(const X &a, const X &b, const X &c)
{
return min_2(min_2(a, b), c);
}
// API search type: min_4 : (a, a, a, a) -> a
// Minimum of four values.
template <typename X>
const X &min_4(const X &a, const X &b, const X &c, const X &d)
{
return min_2(min_3(a, b, c), d);
}
// API search type: min_5 : (a, a, a, a, a) -> a
// Minimum of five values.
template <typename X>
const X &min_5(const X &a, const X &b, const X &c, const X &d, const X &e)
{
return min_3(min_3(a, b, c), d, e);
}
#endif
int main()
{
constexpr int COUNT = 50000000;
#ifdef VARIADIC
std::vector<int> v;
v.reserve(COUNT);
std::cout << "variadic version: " << '\n';
std::cout << measure<std::chrono::milliseconds>::execution([&v]()
{
for (int i = -COUNT; i < COUNT; ++i)
v.emplace_back(min(1, -14, 3, -1, i));
}) << "ms\n";
#else
std::vector<int> v1;
v1.reserve(COUNT);
std::cout << "functional plus version: " << '\n';
std::cout << measure<std::chrono::milliseconds>::execution([&v1]()
{
for (int i = -COUNT; i < COUNT; ++i)
v1.emplace_back(min_5(1, -14, 3, -1, i));
}) << "ms\n";
#endif
}
I have received the following results:
/usr/bin/time g++ -g -Wall -Wpedantic -Wextra --std=c++14 main.cpp
0.26user 0.01system 0:00.28elapsed 97%CPU (0avgtext+0avgdata 68492maxresident)k
0inputs+264outputs (0major+19034minor)pagefaults 0swaps
/usr/bin/time g++ -DVARIADIC -g -Wall -Wpedantic -Wextra --std=c++14 main.cpp
0.26user 0.01system 0:00.29elapsed 97%CPU (0avgtext+0avgdata 68812maxresident)k
0inputs+264outputs (0major+19567minor)pagefaults 0swaps
/usr/bin/time g++ -O3 -Wall -Wpedantic -Wextra --std=c++14 main.cpp
0.24user 0.02system 0:00.26elapsed 97%CPU (0avgtext+0avgdata 72600maxresident)k
0inputs+32outputs (0major+17738minor)pagefaults 0swaps
/usr/bin/time g++ -DVARIADIC -O3 -Wall -Wpedantic -Wextra --std=c++14 main.cpp
0.24user 0.01system 0:00.26elapsed 96%CPU (0avgtext+0avgdata 71140maxresident)k
0inputs+32outputs (0major+17364minor)pagefaults 0swaps
from functionalplus.
Wow, that's really good! 👍
from functionalplus.
Btw. here's the C++11 port:
template <typename U, typename... V>
auto min(const U &u, const V &... v) -> typename std::common_type<U, V...>::type
{
using rettype = typename std::common_type<U, V...>::type;
rettype result = static_cast<rettype>(u);
(void)std::initializer_list<bool>{((v < result) ? (result = static_cast<rettype>(v), false) : false)...};
return result;
}
from functionalplus.
@pmalek you should definitely open a PR for this.
from functionalplus.
To be clear: you strive for C++11 compatibility here, correct?
Give me a couple of days as I am currently a bit busy for this :)
from functionalplus.
Yes, with the two std::common_type…
changes it compiles fine under C++11.
from functionalplus.
Related Issues (20)
- implement drop_last_while method HOT 2
- Unit tests always succeeding, even when breaking them on purpose HOT 5
- Consecutive build should only re-build changed files instead of all. (Circular build dependency in CMake setup?) HOT 2
- error: exception handling disabled, use ‘-fexceptions’ to enable HOT 1
- keep_by_idx() returns list of empty elements. HOT 4
- Branch `master` will be renamed to `main` in November HOT 1
- 0.2.16-p0: Tests fail to build: "EQUAL" "1" Unknown arguments specified HOT 10
- Cannot compile with gcc 11.2 in C++20 mode HOT 3
- make c++ class function to c pointer HOT 7
- Add int overrides for (some) size_t params as a convenience for fplus::fwd? HOT 3
- fplus::compose with yet unknown output type HOT 3
- parallel_for ? (no, this is not the 1st of April ;-) HOT 3
- IO Monad HOT 5
- composition.hpp uses std::result_of HOT 5
- Function to print std::tuple HOT 5
- Some List Functions (in particular, fplus::transform etc.) do not work with custom container. What needs to be added? HOT 6
- Clang 16: incompatible initializer of type (container_common_test) HOT 3
- FunctionalPlus does not compile with C++20 (std::result_of was removed) HOT 3
- CI fails because of missing GLIBC HOT 8
- Conan packages of this library are often outdated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functionalplus.