Giter VIP home page Giter VIP logo

Comments (7)

offa avatar offa commented on May 24, 2024

Just out of interest: How does the compile time of both compare? Is there a notable difference?

from functionalplus.

pmalek avatar pmalek commented on May 24, 2024

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.

offa avatar offa commented on May 24, 2024

Wow, that's really good! 👍

from functionalplus.

offa avatar offa commented on May 24, 2024

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.

offa avatar offa commented on May 24, 2024

@pmalek you should definitely open a PR for this.

from functionalplus.

pmalek avatar pmalek commented on May 24, 2024

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.

offa avatar offa commented on May 24, 2024

Yes, with the two std::common_type… changes it compiles fine under C++11.

from functionalplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.