#include #include #include #include #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN # include unsigned long __rw_time_now () { return GetTickCount (); } #else # include unsigned long __rw_time_now () { timeval tv; gettimeofday (&tv, 0); return tv.tv_sec * 1000 + tv.tv_usec / 1000; } #endif int main (int argc, char *argv[]) { const int N = 100000000; const std::string x("x"); const std::string::const_iterator b = x.begin(); const std::string::const_iterator e = x.end (); const std::string::const_pointer p = x.c_str (); const char* names[] = { "string::append(string, size_type, size_type)", "string::append(string)", "string::append(const_pointer, size_type)", "string::append(const_pointer)", "string::append(iterator, iterator)", "string::append(size_type, value_type)", "string::operator+=(string)", "string::operator+=(const_pointer)", "string::operator+=(value_type)", "string::push_back(value_type)", }; for (int i = 0; i < 10; ++i) { std::string str; str.reserve(N); unsigned long __start = __rw_time_now (); switch (i) { case 0: for (int j = 0; j < N; ++j) str.append (x, 0, 1); break; case 1: for (int j = 0; j < N; ++j) str.append (x); break; case 2: for (int j = 0; j < N; ++j) str.append (p, 1); break; case 3: for (int j = 0; j < N; ++j) str.append (p); break; case 4: for (int j = 0; j < N; ++j) str.append (b, e); break; case 5: for (int j = 0; j < N; ++j) str.append (1, 'x'); break; case 6: for (int j = 0; j < N; ++j) str += x; break; case 7: for (int j = 0; j < N; ++j) str += p; break; case 8: for (int j = 0; j < N; ++j) str += 'x'; break; case 9: for (int j = 0; j < N; ++j) str.push_back('x'); break; } unsigned long __finish = __rw_time_now (); assert (str.size () == std::size_t (N)); std::printf ("%f [%s]\n", (__finish - __start) / 1000.f, names[i]); } return 0; }