string_conversions.cpp
Go to the documentation of this file.
1 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
17 #include <iostream>
18 #include <sstream>
19 #include <ecl/threads/priority.hpp>
20 #include <ecl/time/stopwatch.hpp>
21 #include <ecl/time/timestamp.hpp>
22 #include <ecl/converters.hpp>
23 
24 //#include <fastformat/fastformat.hpp>
25 //#include <fastformat/sinks/char_buffer.hpp>
26 
27 /*****************************************************************************
28 ** Using
29 *****************************************************************************/
30 
31 using std::string;
32 using std::ostringstream;
33 using ecl::StopWatch;
34 using ecl::TimeStamp;
35 using ecl::Converter;
37 
38 /*****************************************************************************
39 ** Main
40 *****************************************************************************/
41 
42 
43 int main()
44 {
45  try {
46  ecl::set_priority(ecl::RealTimePriority4);
47  } catch ( StandardException &e ) {
48  // dont worry about it.
49  }
50  StopWatch stopwatch;
51  TimeStamp time_converters[10], time_sprintf[10], time_iostreams[10];
52 
53  string str;
54  int i = -11;
55  unsigned int ui = 123;
56  long l = -211123;
57  float f = -16.123f;
58  double d = -2316.1234;
59  char buffer[30];
60  char* char_string;
61  Converter<char*> toCharString;
62 // Converter<string> toString;
63  ostringstream ostream;
64 
65  for (int j = 0; j < 50; ++j ) {
66  sprintf(buffer,"%d",i); // First sprintf is always slow, it caches some crap I think.
67  sprintf(buffer,"%f",f); // Like above, but for floats.
68  ostream << i; // Ditto
69  ostream << l; // Ditto
70  }
71 
72  /*********************
73  ** Sprintf
74  **********************/
75  stopwatch.restart();
76  sprintf(buffer,"%d",i);
77  time_sprintf[0] = stopwatch.split();
78  sprintf(buffer,"%d",ui);
79  time_sprintf[1] = stopwatch.split();
80  sprintf(buffer,"%ld",l);
81  time_sprintf[2] = stopwatch.split();
82  sprintf(buffer,"%f",f);
83  time_sprintf[3] = stopwatch.split();
84  sprintf(buffer,"%lf",d);
85  time_sprintf[4] = stopwatch.split();
86 
87  /*********************
88  ** IOStreams
89  **********************/
90  stopwatch.restart();
91  ostream << i;
92  time_iostreams[0] = stopwatch.split();
93  ostream << ui;
94  time_iostreams[1] = stopwatch.split();
95  ostream << l;
96  time_iostreams[2] = stopwatch.split();
97  ostream << f;
98  time_iostreams[3] = stopwatch.split();
99  ostream << d;
100  time_iostreams[4] = stopwatch.split();
101 
102 // /*********************
103 // ** FastFormat
104 // **********************/
105 // char char_buffer[250];
106 // TimeStamp time_ff[10];
107 // fastformat::sinks::char_buffer_sink ff_char_buffer(250,char_buffer);
108 // fastformat::write(ff_char_buffer, i);
109 // fastformat::write(ff_char_buffer, l);
110 // stopwatch.restart();
111 // fastformat::write(ff_char_buffer, i);
112 // time_ff[0] = stopwatch.split();
113 // fastformat::write(ff_char_buffer, l);
114 // time_ff[1] = stopwatch.split();
116 // time_ff[2] = stopwatch.split();
118 // time_ff[3] = stopwatch.split();
119 //
120  /*********************
121  ** Converter
122  **********************/
123  char_string = toCharString(f);
124  char_string = toCharString(f);
125  char_string = toCharString(f);
126  char_string = toCharString(f);
127  stopwatch.restart();
128  char_string = toCharString(i);
129  time_converters[0] = stopwatch.split();
130  char_string = toCharString(ui);
131  time_converters[1] = stopwatch.split();
132  char_string = toCharString(l);
133  time_converters[2] = stopwatch.split();
134  char_string = toCharString(f);
135  time_converters[3] = stopwatch.split();
136  char_string = toCharString(d);
137  time_converters[4] = stopwatch.split();
138 
139 
140  std::cout << std::endl;
141  std::cout << "***********************************************************" << std::endl;
142  std::cout << " Performance comparison of char string conversion apis" << std::endl;
143  std::cout << "***********************************************************" << std::endl;
144  std::cout << std::endl;
145 
146  std::cout << "Converter<char*>(int) " << " Time: " << time_converters[0] << std::endl;
147  std::cout << "Converter<char*>(unsigned int) " << " Time: " << time_converters[1] << std::endl;
148  std::cout << "Converter<char*>(long) " << " Time: " << time_converters[2] << std::endl;
149  std::cout << "Converter<char*>(float) " << " Time: " << time_converters[3] << std::endl;
150  std::cout << "Converter<char*>(double) " << " Time: " << time_converters[4] << std::endl;
151  std::cout << std::endl;
152  std::cout << "sprintf(int) " << " Time: " << time_sprintf[0] << std::endl;
153  std::cout << "sprintf(unsigned int) " << " Time: " << time_sprintf[1] << std::endl;
154  std::cout << "sprintf(long) " << " Time: " << time_sprintf[2] << std::endl;
155  std::cout << "sprintf(float) " << " Time: " << time_sprintf[3] << std::endl;
156  std::cout << "sprintf(double) " << " Time: " << time_sprintf[4] << std::endl;
157  std::cout << std::endl;
158  std::cout << "ostringstream(int) " << " Time: " << time_iostreams[0] << std::endl;
159  std::cout << "ostringstream(unsigned int) " << " Time: " << time_iostreams[1] << std::endl;
160  std::cout << "ostringstream(long) " << " Time: " << time_iostreams[2] << std::endl;
161  std::cout << "ostringstream(float) " << " Time: " << time_iostreams[3] << std::endl;
162  std::cout << "ostringstream(double) " << " Time: " << time_iostreams[4] << std::endl;
163  std::cout << std::endl;
164 // std::cout << "fastformat(int) " << " Time: " << time_ff[0] << std::endl;
165 // std::cout << "fastformat(long) " << " Time: " << time_ff[1] << std::endl;
166 // std::cout << "fastformat(float) " << " Time: " << time_ff[2] << std::endl;
167 // std::cout << "fastformat(double) " << " Time: " << time_ff[3] << std::endl;
168 // std::cout << std::endl;
169 
170  return 0;
171 }
172 
int main()
RealTimePriority4
void d()
void f(int i) ecl_debug_throw_decl(StandardException)


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:55