[Assorted-commits] SF.net SVN: assorted:[1171] serialization-bench/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-06 05:00:49
|
Revision: 1171 http://assorted.svn.sourceforge.net/assorted/?rev=1171&view=rev Author: yangzhang Date: 2009-02-06 05:00:42 +0000 (Fri, 06 Feb 2009) Log Message: ----------- rewrote everything to increase flexibility and reuse Modified Paths: -------------- serialization-bench/trunk/src/Makefile serialization-bench/trunk/src/main.cc Modified: serialization-bench/trunk/src/Makefile =================================================================== --- serialization-bench/trunk/src/Makefile 2009-02-06 00:41:10 UTC (rev 1170) +++ serialization-bench/trunk/src/Makefile 2009-02-06 05:00:42 UTC (rev 1171) @@ -1,7 +1,10 @@ +WTF := wtf +CXX := $(WTF) $(CXX) + all: main main: main.cc test.pb.h test.pb.cc - g++ -O3 -I. -g3 -Wall -o main main.cc test.pb.cc \ + $(CXX) -O3 -I. -g3 -Wall -o main main.cc test.pb.cc \ -lboost_serialization-gcc43-mt -lprotobuf test.pb.cc test.pb.h: test.proto Modified: serialization-bench/trunk/src/main.cc =================================================================== --- serialization-bench/trunk/src/main.cc 2009-02-06 00:41:10 UTC (rev 1170) +++ serialization-bench/trunk/src/main.cc 2009-02-06 05:00:42 UTC (rev 1171) @@ -5,80 +5,152 @@ #include <commons/time.h> #include <fstream> #include <iostream> +#include <sstream> #include <sys/types.h> #include <sys/stat.h> +#include <typeinfo> #include <unistd.h> using namespace boost::archive; using namespace commons; using namespace std; -int main() { - int count = 1e6, reps = 3; - cout << "TIMES" << endl; - for (int r = 0; r < reps; r++) { - ofstream o("raw.out", ios::out); +int cnt = 1e6, reps = 3; + +struct exp { + void time() { + build(); long long start = current_time_millis(); - for (int i = 0; i < count; i++) { + run(); + long long end = current_time_millis(); + cout << name << ": " << end - start << " ms, " << size() << " b" << endl; + } + virtual void build() {} + virtual void run() = 0; + virtual size_t size() { return 0; } // XXX + exp &named(const string &n) { this->name = n; return *this; } + string name; +}; + +struct streambuf_exp : virtual exp { + stringbuf b; +}; + +struct ostream_exp : virtual exp { + stringstream o; +}; + +struct ostream_write : virtual ostream_exp { + void run() { + for (int i = 0; i < cnt; ++i) { o.write(reinterpret_cast<char*>(&i), sizeof i); } - long long end = current_time_millis(); - cout << "raw: " << end - start << endl; } - for (int r = 0; r < reps; r++) { - ofstream o("htonl.out", ios::out); - long long start = current_time_millis(); - for (int i = 0; i < count; i++) { - int n = htonl(i); - o.write(reinterpret_cast<char*>(&n), sizeof i); +}; + +struct streambuf_sputn : virtual streambuf_exp { + void run() { + for (int i = 0; i < cnt; ++i) { + b.sputn(reinterpret_cast<char*>(&i), sizeof i); } - long long end = current_time_millis(); - cout << "htonl: " << end - start << endl; } - for (int r = 0; r < reps; r++) { - ofstream o("fstream.out", ios::out); - long long start = current_time_millis(); - for (int i = 0; i < count; i++) { +}; + +struct streambuf_sputn_htonl : virtual streambuf_exp { + void run() { + for (int i = 0; i < cnt; ++i) { int n = htonl(i); - o << n << " "; + b.sputn(reinterpret_cast<char*>(&n), sizeof i); } - long long end = current_time_millis(); - cout << "fstream: " << end - start << endl; } - for (int r = 0; r < reps; r++) { - filebuf b; - b.open("boost.out", ios::out); - binary_oarchive o(b); - long long start = current_time_millis(); - for (int i = 0; i < count; i++) { - o & i; +}; + +struct ostream_fmt : virtual ostream_exp { + void run() { + for (int i = 0; i < cnt; ++i) { + o << i << ' '; } - long long end = current_time_millis(); - cout << "boost: " << end - start << endl; } - for (int r = 0; r < reps; r++) { - ofstream o("protobuf.out", ios::out); - varray a; - long long start = current_time_millis(); - for (int i = 0; i < count; i++) { - a.add_v(i); +}; + +struct streambuf_boost : virtual streambuf_exp { + binary_oarchive a; + streambuf_boost() : a(b) {} + void run() { + for (int i = 0; i < cnt; ++i) { + a << i; } - a.SerializeToOstream(&o); - long long end = current_time_millis(); - cout << "protobuf: " << end - start << endl; } - for (int r = 0; r < reps; r++) { - ofstream o("manyprotos.out", ios::out); - long long start = current_time_millis(); - for (int i = 0; i < count; i++) { - varray a; +}; + +struct protobuf : virtual exp { + varray a; + void build() { + for (int i = 0; i < cnt; ++i) { a.add_v(i); - a.SerializeToOstream(&o); } - long long end = current_time_millis(); - cout << "many protobufs: " << end - start << endl; } +}; +struct ostream_protobuf : virtual ostream_exp, virtual protobuf { + void run() { a.SerializeToOstream(&o); } +}; + +struct protobuf_string : virtual protobuf { + string s; + void run() { a.SerializeToString(&s); } +}; + +struct protobuf_array : virtual protobuf { + char *s; + protobuf_array() : s(new char[2 * cnt * sizeof(int)]) {} + void run() { a.SerializeToArray(s, 2 * cnt * sizeof(int)); } +}; + +struct protobufs_string : virtual exp { + vector<varray> as; + string s; + protobufs_string() : as(cnt) {} + void build() { + for (int i = 0; i < cnt; ++i) { + as[i].add_v(i); + } + } + void run() { + for (int i = 0; i < cnt; ++i) { + as[i].SerializeToString(&s); + } + } +}; + +struct raw_array : virtual exp { + char *a; + raw_array() : a(new char[cnt * sizeof(int)]) {} + void run() { + int *p = (int*) a; + for (int i = 0; i < cnt; ++i) { + p[i] = i; + } + } +}; + +#define rep(x) \ + for (int r = 0; r < reps; ++r) \ + x().named(#x).time(); + +int main() { + rep(ostream_write); + rep(streambuf_sputn); + rep(streambuf_sputn_htonl); + rep(ostream_fmt); + rep(streambuf_boost); + rep(ostream_protobuf); + rep(protobuf_string); + rep(protobuf_array); + rep(protobufs_string); + rep(raw_array); + +#if 0 cout << "SIZES" << endl; vector<string> paths; paths.push_back("raw.out"); @@ -92,5 +164,7 @@ stat(paths[i].c_str(), &s); cout << paths[i] << ": " << s.st_size << endl; } +#endif + return 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |