Skip to content

Instantly share code, notes, and snippets.

@wstucco
Created December 25, 2014 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wstucco/b0e2665a26b3fd889435 to your computer and use it in GitHub Desktop.
Save wstucco/b0e2665a26b3fd889435 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2012, Tino Didriksen Consult
* Developed by Tino Didriksen <tino@didriksen.cc>
* Modified by Massimo Ronca
*
* This file is part of Benchmarks
*
* Benchmarks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Benchmarks is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Benchmarks. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include <vector>
int main() {
int *c = (int*)malloc(sizeof(*c));
int *old_c = c;
size_t c_cap = 1;
size_t cnt_c = 0;
std::vector<int> v(1);
int *old_v = &v[0];
size_t cnt_v = 0;
for (size_t i=0 ; i<10000000 ; ++i) {
if(i == c_cap) {
c_cap *= 2;
c = (int*)realloc(c, sizeof(*c)*c_cap);
if (old_c != c) {
++cnt_c;
}
old_c = c;
}
c[i] = i;
v.push_back(i);
if (old_v != &v[0]) {
++cnt_v;
}
old_v = &v[0];
}
std::cout << "realloc() reallocations: " << cnt_c << std::endl;
std::cout << "std::vector reallocations: " << cnt_v << std::endl;
free(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment