Okay here is some some source code to reproduce the effect (see below) which was compiled with
emcc vectorspeed.cpp -std=c++14 -o index.html -s TOTAL_MEMORY=67108864
adding -O2 will produce exception/hangs on Microsoft Browsers
results in Firefox 46.0.1:
malloc/free: 0.001s
vector ctor: 0.398s
vector resize: 0.192s
vector reserve: 0s
results in IE 11:
malloc/free: 0.001s
vector ctor: 2.063s !!!!!
vector resize: 1.321s
vector reserve: 0s
results in Edge:
malloc/free: 0.002s
vector ctor: 1.634s !!!!!
vector resize: 0.787s
vector reserve: 0.001s
Source vectorspeed.cpp:
#include<iostream>
#include<vector>
#include<chrono>
using double_sec = std::chrono::duration<double, std::ratio<1>>;
template <typename Func,typename... Args>
double profile(Func f, Args&&... args)
{
using namespace std::chrono;
auto start = system_clock::now();
f(std::forward<Args>(args)...);
return duration_cast<double_sec>(system_clock::now() - start).count();
}
int main() {
std::size_t n = 48000000;
auto t1 = profile([](std::size_t n)
{
void* ptr = malloc(n);
free(ptr);
}, n);
auto t2 = profile([](std::size_t n)
{
std::vector<unsigned char> vec(n);
}, n);
auto t3 = profile([](std::size_t n)
{
std::vector<unsigned char> vec;
vec.resize(n);
}, n);
auto t4 = profile([](std::size_t n)
{
std::vector<unsigned char> vec;
vec.reserve(n);
}, n);
std::cout << "malloc/free: " << t1 << "s"<< std::endl;
std::cout << "vector ctor: " << t2 << "s" << std::endl;
std::cout << "vector resize: " << t3 << "s" << std::endl;
std::cout << "vector reserve: " << t4 << "s" << std::endl;
return 0;
}