Skip to content

Instantly share code, notes, and snippets.

@zrax
Last active November 14, 2015 01:07
Show Gist options
  • Save zrax/6a23bf162b4d4acfd8cf to your computer and use it in GitHub Desktop.
Save zrax/6a23bf162b4d4acfd8cf to your computer and use it in GitHub Desktop.
plString performance
/*
GCC -O0 GCC -O3 Clang -O0 Clang -O3 VC12 Dbg VC12 Rel
Nothing : 3.86 ms 0.44 ms 3.02 ms 0.40 ms 0.98 ms 0.00 ms
Empty plString : 5.27 ms 0.44 ms 5.20 ms 0.37 ms 5.86 ms 0.98 ms
Empty std::string : 2.55 ms 0.44 ms 2.05 ms 0.37 ms 112.38 ms 0.00 ms
Short plString : 9.57 ms 2.94 ms 7.40 ms 2.94 ms 22.48 ms 2.93 ms
Short std::string : 5.71 ms 0.44 ms 4.98 ms 0.37 ms 119.22 ms 1.95 ms
Long plString : 17.86 ms 11.05 ms 15.99 ms 10.11 ms 248.21 ms 214.01 ms
Long std::string : 9.91 ms 3.58 ms 8.89 ms 3.82 ms 237.46 ms 111.40 ms
Copy short plString : 5.50 ms 0.34 ms 4.13 ms 0.27 ms 5.86 ms 0.98 ms
Copy short std::string : 4.60 ms 1.15 ms 4.42 ms 1.09 ms 115.31 ms 1.95 ms
Copy long plString : 6.89 ms 0.37 ms 4.74 ms 0.31 ms 6.84 ms 0.98 ms
Copy long std::string : 8.95 ms 4.03 ms 7.86 ms 4.47 ms 204.23 ms 123.13 ms
static snprintf : 41.94 ms 37.64 ms 39.27 ms 39.47 ms 232.58 ms 120.21 ms
dynamic snprintf : 85.90 ms 79.86 ms 81.74 ms 83.93 ms 497.40 ms 270.67 ms
plString::Format : 59.14 ms 47.94 ms 57.55 ms 47.20 ms 465.15 ms 210.10 ms
plFormat : 130.62 ms 83.01 ms 128.73 ms 80.62 ms 3382.12 ms 1211.74 ms
plStringStream (~format) : 70.28 ms 53.65 ms 68.12 ms 51.55 ms ? ms ? ms
boost::format : 874.06 ms 297.22 ms 902.48 ms 323.40 ms ? ms ? ms
*/
#include <chrono>
#include <functional>
#include "plFormat.h"
static void _measure(std::function<void()> fun, const char *title)
{
auto clk = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 100000; ++i)
fun();
auto dur = std::chrono::high_resolution_clock::now() - clk;
puts(plFormat("{32}: {6.2f} ms", title,
std::chrono::duration<double, std::milli>(dur).count()).c_str());
}
/* Former plString::Format() */
static plString plString__Format(const char *fmt, ...)
{
va_list vptr;
va_start(vptr, fmt);
plString str = plString::IFormat(fmt, vptr);
va_end(vptr);
return str;
}
int main(int, char **)
{
_measure([]() { }, "Nothing");
_measure([]() {
plString s;
volatile const char *V = s.c_str();
}, "Empty plString");
_measure([]() {
std::string s;
volatile const char *V = s.c_str();
}, "Empty std::string");
_measure([]() {
plString shortStr("Short");
volatile const char *V = shortStr.c_str();
}, "Short plString");
_measure([]() {
std::string shortStr("Short");
volatile const char *V = shortStr.c_str();
}, "Short std::string");
_measure([]() {
plString longStr("This is a long string. Testing the (unsigned) long long string.");
volatile const char *V = longStr.c_str();
}, "Long plString");
_measure([]() {
std::string longStr("This is a long string. Testing the (unsigned) long long string.");
volatile const char *V = longStr.c_str();
}, "Long std::string");
plString _xc1("Short");
_measure([&_xc1]() {
plString copy = _xc1;
volatile const char *V = copy.c_str();
}, "Copy short plString");
std::string _xc2("Short");
_measure([&_xc2]() {
std::string copy = _xc2;
volatile const char *V = copy.c_str();
}, "Copy short std::string");
plString _xc3("This is a long string. Testing the (unsigned) long long string.");
_measure([&_xc3]() {
plString copy = _xc3;
volatile const char *V = copy.c_str();
}, "Copy long plString");
std::string _xc4("This is a long string. Testing the (unsigned) long long string.");
_measure([&_xc4]() {
std::string copy = _xc4;
volatile const char *V = copy.c_str();
}, "Copy long std::string");
_measure([]() {
char buffer[256];
snprintf(buffer, 256, "This %d is %6.2f a %s test %c.", 42, M_PI, "<Singin' in the rain>", '?');
volatile const char *V = buffer;
}, "static snprintf");
_measure([]() {
int len = snprintf(nullptr, 0, "This %d is %6.2f a %s test %c.", 42, M_PI, "<Singin' in the rain>", '?');
char *buffer = new char[len + 1];
snprintf(buffer, len, "This %d is %6.2f a %s test %c.", 42, M_PI, "<Singin' in the rain>", '?');
volatile const char *V = buffer;
delete[] buffer;
}, "dynamic snprintf");
_measure([]() {
plString foo = plString__Format("This %d is %6.2f a %s test %c.", 42, M_PI, "<Singin' in the rain>", '?');
volatile const char *V = foo.c_str();
}, "plString::Format");
_measure([]() {
plString foo = plFormat("This {} is {6.2f} a {} test {}.", 42, M_PI, "<Singin' in the rain>", '?');
volatile const char *V = foo.c_str();
}, "plFormat");
_measure([]() {
plStringStream ss;
ss << "This " << 42 << " is " << M_PI << " a "
<< "<Singin' in the rain>" << " test " << '?' << ".";
volatile const char *V = ss.GetString().c_str();
}, "plStringStream (~format)");
_measure([]() {
std::string foo = (boost::format("This %1% is %2$6.2f a %3% test %4%.")
% 42 % M_PI % "<Singin' in the rain>" % '?').str();
volatile const char *V = foo.c_str();
}, "boost::format");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment