Created
February 7, 2016 18:35
-
-
Save ldionne/2277947fb9fc82fe6c02 to your computer and use it in GitHub Desktop.
Benchmark of different compile-time string representations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright Louis Dionne 2013-2016 | |
// Distributed under the Boost Software License, Version 1.0. | |
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
template <char ...s> | |
static auto string_maker = [] { | |
struct local { char data[sizeof...(s)] = {s...}; }; | |
return local{}; | |
}; | |
template <char ...s> | |
decltype(string_maker<s...>()) string{}; | |
int main() { | |
<% ('a'..'f').each do |c| %> | |
auto s_<%= c %> = string< | |
<%= input_size.times.map { "'#{c}'" }.join(', ') %> | |
>; | |
(void)s_<%= c %>; | |
<% end %> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright Louis Dionne 2013-2016 | |
// Distributed under the Boost Software License, Version 1.0. | |
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
template <char ...> | |
int string = 0; | |
int main() { | |
<% ('a'..'f').each do |c| %> | |
auto s_<%= c %> = string< | |
<%= input_size.times.map { "'#{c}'" }.join(', ') %> | |
>; | |
(void)s_<%= c %>; | |
<% end %> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
def benchmark(variant) | |
time_compilation("#{variant}.erb.cpp", (1...1000).step(25)) | |
end | |
%> | |
{ | |
"title": { | |
"text": "Compile-time behavior of creating a string" | |
}, | |
"xAxis": { | |
"title": { "text": "Size of the string" } | |
}, | |
"series": [ | |
{ | |
"name": "baseline", | |
"data": <%= benchmark("baseline") %> | |
}, { | |
"name": "anonymous type", | |
"data": <%= benchmark("anonymous") %> | |
}, { | |
"name": "struct", | |
"data": <%= benchmark("struct") %> | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright Louis Dionne 2013-2016 | |
// Distributed under the Boost Software License, Version 1.0. | |
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
template <char ...s> | |
struct string { | |
char value[sizeof...(s)] = {s...}; | |
}; | |
int main() { | |
<% ('a'..'f').each do |c| %> | |
auto s_<%= c %> = string< | |
<%= input_size.times.map { "'#{c}'" }.join(', ') %> | |
>{}; | |
(void)s_<%= c %>; | |
<% end %> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment