简体   繁体   中英

Check the tuple each element has a value

I have a std::tuple< std::optional<Args>... > , need to check that every element has a value. I have implemented it with std::index_sequence. But I'm not sure this is the most efficient solution for fastest compile time.

using data_type = std::tuple< std::optional<Arg_1>, 
                              std::optional<Arg_2>,
                               //....
                              std::optional<Arg_n>  // where n > 40
                             >;

// My solution.
template <size_t ... indexes>
bool has_value_all_elements_impl(const data_type& tuple_data, std::index_sequence<indexes ...> ) {
       //I assume there O(n) lookup for each index in compile time. 
       // So total O(n^2) lookup for tuple ?
       return (std::get<indexes>(tuple_data).has_value() && ... ) ;
}

bool has_value_all_elements(data_type const& tuple_data)
{
     return has_value_all_elements_impl(
      tuple_data, std::make_index_sequence<std::tuple_size<data_type>::value>{});
}

Is there a more efficient O(n) algorithm for such problem?

Or is my solution already O(n)?

I decided answer my own question.

Seems @chronial comment is right.

source code on godbold - Seems, it depends STL implementation. GCC stdlibc++ increased compile time is not linear. libc++ increased compile time is linear.

GCC:

    $ g++ --version
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


 
$ time g++ -DARG_SIZE=10 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    0m0.431s
user    0m0.384s
sys 0m0.047s


$ time g++ -DARG_SIZE=20 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    0m0.507s
user    0m0.461s
sys 0m0.046s

$ time g++ -DARG_SIZE=40 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    0m0.737s
user    0m0.666s
sys 0m0.071s

$ time g++ -DARG_SIZE=80 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    0m2.030s
user    0m1.882s
sys 0m0.148s

$ time g++ -DARG_SIZE=160 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    0m23.532s
user    0m23.216s
sys 0m0.312s

$ time g++ -DARG_SIZE=320 -Wall -o "example" "example.cpp" -std=c++17     -lpthread  -Wall -Wextra -pedantic -O2

real    10m40.392s
user    10m37.992s
sys 0m2.067s

CLang:

$ clang++ --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin


$ time clang++ -DARG_SIZE=10 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m1.056s
user    0m0.667s
sys 0m0.066s

$ time clang++ -DARG_SIZE=20 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m0.816s
user    0m0.764s
sys 0m0.048s

$ time clang++ -DARG_SIZE=40 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m1.455s
user    0m0.985s
sys 0m0.095s

$ time clang++ -DARG_SIZE=80 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m1.565s
user    0m1.484s
sys 0m0.067s

$ time clang++ -DARG_SIZE=160 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m3.172s
user    0m2.410s
sys 0m0.080s

$ time clang++ -DARG_SIZE=320 -Wall -o "example" "example.cpp" -std=c++17  -stdlib=libc++   -lpthread  -Wall -Wextra -pedantic -O2 -fbracket-depth=400
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
    data_type d;
              ^
1 warning generated.

real    0m4.576s
user    0m4.337s
sys 0m0.238s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM