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(...) \
30 pw::string::Format(buffer, __VA_ARGS__) \
31 .IgnoreError() // TODO: b/242598609 - Handle Status properly
32
33 #else // std::snprintf
34
35 #include <cstdio>
36
37 #define FORMAT_CASE(...) std::snprintf(buffer, buffer_size, __VA_ARGS__)
38
39 #endif // USE_FORMAT
40
41 namespace pw::string {
42
43 char buffer_1[128];
44 char buffer_2[128];
45
46 char* volatile get_buffer_1 = buffer_1;
47 char* volatile get_buffer_2 = buffer_2;
48 volatile unsigned get_size;
49
OutputStringsToBuffer()50 void OutputStringsToBuffer() {
51 #if USE_FORMAT
52 auto buffer = span(get_buffer_1, get_size);
53 #else
54 char* buffer = get_buffer_1;
55 unsigned buffer_size = get_size;
56 #endif // USE_FORMAT
57
58 const char* string = get_buffer_2;
59 unsigned value = get_size;
60
61 FORMAT_CASE("The quick brown");
62 FORMAT_CASE("%s", string);
63 FORMAT_CASE("jumped over the %s d%ug.", string, value);
64 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
65 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
66 FORMAT_CASE("The quick brown");
67 FORMAT_CASE("%s", string);
68 FORMAT_CASE("jumped over the %s d%ug.", string, value);
69 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
70 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
71
72 FORMAT_CASE("The quick brown");
73 FORMAT_CASE("%s", string);
74 FORMAT_CASE("jumped over the %s d%ug.", string, value);
75 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
76 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
77 FORMAT_CASE("The quick brown");
78 FORMAT_CASE("%s", string);
79 FORMAT_CASE("jumped over the %s d%ug.", string, value);
80 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
81 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
82
83 FORMAT_CASE("The quick brown");
84 FORMAT_CASE("%s", string);
85 FORMAT_CASE("jumped over the %s d%ug.", string, value);
86 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
87 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
88 FORMAT_CASE("The quick brown");
89 FORMAT_CASE("%s", string);
90 FORMAT_CASE("jumped over the %s d%ug.", string, value);
91 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
92 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
93
94 FORMAT_CASE("The quick brown");
95 FORMAT_CASE("%s", string);
96 FORMAT_CASE("jumped over the %s d%ug.", string, value);
97 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
98 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
99 FORMAT_CASE("The quick brown");
100 FORMAT_CASE("%s", string);
101 FORMAT_CASE("jumped over the %s d%ug.", string, value);
102 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
103 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
104
105 FORMAT_CASE("The quick brown");
106 FORMAT_CASE("%s", string);
107 FORMAT_CASE("jumped over the %s d%ug.", string, value);
108 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
109 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
110 FORMAT_CASE("The quick brown");
111 FORMAT_CASE("%s", string);
112 FORMAT_CASE("jumped over the %s d%ug.", string, value);
113 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
114 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
115 }
116
117 } // namespace pw::string
118
main()119 int main() {
120 pw::string::OutputStringsToBuffer();
121 return 0;
122 }
123