1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 // This size report uses pw::string::Format and std::snprintf to write to the
16 // same buffer numerous times. No error handling or size checking is done.
17 //
18 // This compares the overhead of calling pw::string::Format with a span to
19 // calling std::snprintf with a separate pointer and buffer size.
20 
21 #ifndef USE_FORMAT
22 #error "USE_FORMAT must be defined"
23 #endif  // USE_FORMAT
24 
25 #if USE_FORMAT
26 
27 #include "pw_string/format.h"
28 
29 #define FORMAT_CASE(...) pw::string::Format(buffer, __VA_ARGS__)
30 
31 #else  // std::snprintf
32 
33 #include <cstdio>
34 
35 #define FORMAT_CASE(...) std::snprintf(buffer, buffer_size, __VA_ARGS__)
36 
37 #endif  // USE_FORMAT
38 
39 namespace pw::string {
40 
41 char buffer_1[128];
42 char buffer_2[128];
43 
44 char* volatile get_buffer_1 = buffer_1;
45 char* volatile get_buffer_2 = buffer_2;
46 volatile unsigned get_size;
47 
OutputStringsToBuffer()48 void OutputStringsToBuffer() {
49 #if USE_FORMAT
50   auto buffer = std::span(get_buffer_1, get_size);
51 #else
52   char* buffer = get_buffer_1;
53   unsigned buffer_size = get_size;
54 #endif  // USE_FORMAT
55 
56   const char* string = get_buffer_2;
57   unsigned value = get_size;
58 
59   FORMAT_CASE("The quick brown");
60   FORMAT_CASE("%s", string);
61   FORMAT_CASE("jumped over the %s d%ug.", string, value);
62   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
63   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
64   FORMAT_CASE("The quick brown");
65   FORMAT_CASE("%s", string);
66   FORMAT_CASE("jumped over the %s d%ug.", string, value);
67   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
68   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
69 
70   FORMAT_CASE("The quick brown");
71   FORMAT_CASE("%s", string);
72   FORMAT_CASE("jumped over the %s d%ug.", string, value);
73   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
74   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
75   FORMAT_CASE("The quick brown");
76   FORMAT_CASE("%s", string);
77   FORMAT_CASE("jumped over the %s d%ug.", string, value);
78   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
79   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
80 
81   FORMAT_CASE("The quick brown");
82   FORMAT_CASE("%s", string);
83   FORMAT_CASE("jumped over the %s d%ug.", string, value);
84   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
85   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
86   FORMAT_CASE("The quick brown");
87   FORMAT_CASE("%s", string);
88   FORMAT_CASE("jumped over the %s d%ug.", string, value);
89   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
90   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
91 
92   FORMAT_CASE("The quick brown");
93   FORMAT_CASE("%s", string);
94   FORMAT_CASE("jumped over the %s d%ug.", string, value);
95   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
96   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
97   FORMAT_CASE("The quick brown");
98   FORMAT_CASE("%s", string);
99   FORMAT_CASE("jumped over the %s d%ug.", string, value);
100   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
101   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
102 
103   FORMAT_CASE("The quick brown");
104   FORMAT_CASE("%s", string);
105   FORMAT_CASE("jumped over the %s d%ug.", string, value);
106   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
107   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
108   FORMAT_CASE("The quick brown");
109   FORMAT_CASE("%s", string);
110   FORMAT_CASE("jumped over the %s d%ug.", string, value);
111   FORMAT_CASE("One two %s %d %s", "three", 4, "five");
112   FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
113 }
114 
115 }  // namespace pw::string
116 
main()117 int main() {
118   pw::string::OutputStringsToBuffer();
119   return 0;
120 }
121