Last active
August 29, 2015 14:25
-
-
Save daverigby/7b672f0d05aa1f6d823e to your computer and use it in GitHub Desktop.
Compare performance of malloc, new and std::vector::resize().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
int main(int argc, char** argv) { | |
std::string mode(argv[1]); | |
size_t size(std::stoi(argv[2])); | |
if (mode == "malloc") { | |
char* foo = (char*)malloc(size); | |
} else if (mode == "new") { | |
char* foo = new char[size]; | |
} else if (mode == "vector") { | |
std::vector<char> foo; | |
foo.resize(size); | |
} else { | |
std::cerr << "Unknown mode. Valid modes are: malloc, new, vector" << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Numbers from Late 2013 MBP (Haswell):