#include #include #include #include #include #include #include using namespace std; #define timersec(tv) (((static_cast((tv)->tv_sec) * 1000000.0f) + (tv)->tv_usec) / 1000000.0f) int main(int argc, char* argv[]) { unsigned int num_loops= 100000; unsigned int buf_size= 100; if (argc == 2) num_loops= atoi(argv[1]); struct timeval tvstart; struct timeval tvend; struct timezone tz; unsigned int i; char c_oss[buf_size]; ostringstream cpp_oss; memset(c_oss, 0, buf_size); cpp_oss.rdbuf()->pubsetbuf(c_oss, buf_size); /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ gettimeofday(&tvstart, &tz); // start cpp-version for(i= 0; i < num_loops; i++) { cpp_oss << i; cpp_oss.clear(); } gettimeofday(&tvend, &tz); cout << " cpp - version for " << i << " loops: " << timersec(&tvend) - timersec(&tvstart) << "\n"; /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ gettimeofday(&tvstart, &tz); // start c-version for(i= 0; i < num_loops; i++) { snprintf(c_oss, buf_size, "%d", i); memset(c_oss, 0, buf_size); } gettimeofday(&tvend, &tz); cout << " c - version for " << i << " loops: " << timersec(&tvend) - timersec(&tvstart) << "\n"; return 0; }