[英]Check the tuple each element has a value
我有一個std::tuple< std::optional<Args>... >
,需要檢查每個元素是否都有值。 我已經用 std::index_sequence 實現了它。 但我不確定這是最快編譯時間的最有效解決方案。
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>{});
}
對於此類問題,是否有更有效的 O(n) 算法?
或者我的解決方案已經是 O(n)?
我決定回答我自己的問題。
似乎@chronial 評論是正確的。
Godbold 上的源代碼- 似乎取決於 STL 的實現。 GCC stdlibc++ 增加的編譯時間不是線性的。 libc++增加的編譯時間是線性的。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.