string_conversions.cpp
Go to the documentation of this file.
00001 
00013 /*****************************************************************************
00014 ** Includes
00015 *****************************************************************************/
00016 
00017 #include <iostream>
00018 #include <sstream>
00019 #include <ecl/threads/priority.hpp>
00020 #include <ecl/time/stopwatch.hpp>
00021 #include <ecl/time/timestamp.hpp>
00022 #include <ecl/converters.hpp>
00023 
00024 //#include <fastformat/fastformat.hpp>
00025 //#include <fastformat/sinks/char_buffer.hpp>
00026 
00027 /*****************************************************************************
00028 ** Using
00029 *****************************************************************************/
00030 
00031 using std::string;
00032 using std::ostringstream;
00033 using ecl::StopWatch;
00034 using ecl::TimeStamp;
00035 using ecl::Converter;
00036 using ecl::StandardException;
00037 
00038 /*****************************************************************************
00039 ** Main
00040 *****************************************************************************/
00041 
00042 
00043 int main()
00044 {
00045         try {
00046                 ecl::set_priority(ecl::RealTimePriority4);
00047         } catch ( StandardException &e ) {
00048                 // dont worry about it.
00049         }
00050     StopWatch stopwatch;
00051     TimeStamp time_converters[10], time_sprintf[10], time_iostreams[10];
00052 
00053     string str;
00054     int i = -11;
00055     unsigned int ui = 123;
00056     long l = -211123;
00057     float f = -16.123f;
00058     double d = -2316.1234;
00059     char buffer[30];
00060     char* char_string;
00061     Converter<char*> toCharString;
00062 //    Converter<string> toString;
00063     ostringstream ostream;
00064 
00065     for (int j = 0; j < 50; ++j ) {
00066         sprintf(buffer,"%d",i);         // First sprintf is always slow, it caches some crap I think.
00067         sprintf(buffer,"%f",f);         // Like above, but for floats.
00068         ostream << i;                   // Ditto
00069         ostream << l;                   // Ditto
00070     }
00071 
00072     /*********************
00073     ** Sprintf
00074     **********************/
00075     stopwatch.restart();
00076     sprintf(buffer,"%d",i);
00077     time_sprintf[0] = stopwatch.split();
00078     sprintf(buffer,"%d",ui);
00079     time_sprintf[1] = stopwatch.split();
00080     sprintf(buffer,"%ld",l);
00081     time_sprintf[2] = stopwatch.split();
00082     sprintf(buffer,"%f",f);
00083     time_sprintf[3] = stopwatch.split();
00084     sprintf(buffer,"%lf",d);
00085     time_sprintf[4] = stopwatch.split();
00086 
00087     /*********************
00088     ** IOStreams
00089     **********************/
00090     stopwatch.restart();
00091     ostream << i;
00092     time_iostreams[0] = stopwatch.split();
00093     ostream << ui;
00094     time_iostreams[1] = stopwatch.split();
00095     ostream << l;
00096     time_iostreams[2] = stopwatch.split();
00097     ostream << f;
00098     time_iostreams[3] = stopwatch.split();
00099     ostream << d;
00100     time_iostreams[4] = stopwatch.split();
00101 
00102 //    /*********************
00103 //    ** FastFormat
00104 //    **********************/
00105 //    char char_buffer[250];
00106 //    TimeStamp time_ff[10];
00107 //    fastformat::sinks::char_buffer_sink ff_char_buffer(250,char_buffer);
00108 //    fastformat::write(ff_char_buffer, i);
00109 //    fastformat::write(ff_char_buffer, l);
00110 //    stopwatch.restart();
00111 //    fastformat::write(ff_char_buffer, i);
00112 //    time_ff[0] = stopwatch.split();
00113 //    fastformat::write(ff_char_buffer, l);
00114 //    time_ff[1] = stopwatch.split();
00116 //    time_ff[2] = stopwatch.split();
00118 //    time_ff[3] = stopwatch.split();
00119 //
00120     /*********************
00121     ** Converter
00122     **********************/
00123     char_string = toCharString(f);
00124     char_string = toCharString(f);
00125     char_string = toCharString(f);
00126     char_string = toCharString(f);
00127     stopwatch.restart();
00128     char_string = toCharString(i);
00129     time_converters[0] = stopwatch.split();
00130     char_string = toCharString(ui);
00131     time_converters[1] = stopwatch.split();
00132     char_string = toCharString(l);
00133     time_converters[2] = stopwatch.split();
00134     char_string = toCharString(f);
00135     time_converters[3] = stopwatch.split();
00136     char_string = toCharString(d);
00137     time_converters[4] = stopwatch.split();
00138 
00139 
00140     std::cout << std::endl;
00141     std::cout << "***********************************************************" << std::endl;
00142     std::cout << "  Performance comparison of char string conversion apis" << std::endl;
00143     std::cout << "***********************************************************" << std::endl;
00144     std::cout << std::endl;
00145 
00146     std::cout << "Converter<char*>(int)          " << " Time: " << time_converters[0] <<  std::endl;
00147     std::cout << "Converter<char*>(unsigned int) " << " Time: " << time_converters[1] <<  std::endl;
00148     std::cout << "Converter<char*>(long)         " << " Time: " << time_converters[2] <<  std::endl;
00149     std::cout << "Converter<char*>(float)        " << " Time: " << time_converters[3] << std::endl;
00150     std::cout << "Converter<char*>(double)       " << " Time: " << time_converters[4] << std::endl;
00151     std::cout << std::endl;
00152     std::cout << "sprintf(int)                   " << " Time: " << time_sprintf[0] <<  std::endl;
00153     std::cout << "sprintf(unsigned int)          " << " Time: " << time_sprintf[1] <<  std::endl;
00154     std::cout << "sprintf(long)                  " << " Time: " << time_sprintf[2] <<  std::endl;
00155     std::cout << "sprintf(float)                 " << " Time: " << time_sprintf[3] <<  std::endl;
00156     std::cout << "sprintf(double)                " << " Time: " << time_sprintf[4] <<  std::endl;
00157     std::cout << std::endl;
00158     std::cout << "ostringstream(int)             " << " Time: " << time_iostreams[0] <<  std::endl;
00159     std::cout << "ostringstream(unsigned int)    " << " Time: " << time_iostreams[1] <<  std::endl;
00160     std::cout << "ostringstream(long)            " << " Time: " << time_iostreams[2] <<  std::endl;
00161     std::cout << "ostringstream(float)           " << " Time: " << time_iostreams[3] <<  std::endl;
00162     std::cout << "ostringstream(double)          " << " Time: " << time_iostreams[4] <<  std::endl;
00163     std::cout << std::endl;
00164 //    std::cout << "fastformat(int)                " << " Time: " << time_ff[0] <<  std::endl;
00165 //    std::cout << "fastformat(long)               " << " Time: " << time_ff[1] <<  std::endl;
00166 //    std::cout << "fastformat(float)              " << " Time: " << time_ff[2] <<  std::endl;
00167 //    std::cout << "fastformat(double)             " << " Time: " << time_ff[3] <<  std::endl;
00168 //    std::cout << std::endl;
00169 
00170     return 0;
00171 }
00172 


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:59