Well, this is a pretty useless test that only measures the selection of small objects. However, simple changes made me shorten the time from about 15 seconds to about 4 seconds. A new version:
typedef vector<string, boost::pool_allocator<string> > str_vector; void testvector(int x, str_vector::iterator it, str_vector::iterator end) { char aux[25] = "X-"; int a = x * 2000; for (; it != end; ++a) { sprintf(aux+2, "%d", a); *it++ = aux; } } int main(int argc, char** argv) { str_vector v(2000); for (int i = 0; i < 10000; i++) { if (i % 1000 == 0) cout << i << endl; testvector(i, v.begin(), v.begin()+2000); } return 0; } real 0m4.089s user 0m3.686s sys 0m0.000s
The Java version has time:
real 0m2.923s user 0m2.490s sys 0m0.063s
(This is my direct java port of your source program, except that it passes an ArrayList as a parameter to reduce useless distributions).
So, to summarize, small allocations are faster in java, and memory management is a bit more complicated in C ++. But we already knew that :)
source share