Skip to content

Instantly share code, notes, and snippets.

@ldionne
Created February 7, 2016 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldionne/2277947fb9fc82fe6c02 to your computer and use it in GitHub Desktop.
Save ldionne/2277947fb9fc82fe6c02 to your computer and use it in GitHub Desktop.
Benchmark of different compile-time string representations
// 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 %>
}
// 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 %>
}
<%
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") %>
}
]
}
// 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