C++ std::allocator 与new对比效率使用
#include <iostream>
#include <vector>
#include <string>
#define allocate_length 100000
int main()
{
std::clock_t start = 0, end = 0;
start = clock();
std::string *str1 = new std::string[allocate_length];
auto str6=str1;
for (int i = 0; i < allocate_length; i++)
{
*str1++ = "Hello World";
}
delete []str6;
end = clock();
std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl;
start = clock();
std::allocator<std::string> str_allocate;
std::string *str3 = str_allocate.allocate(allocate_length);
auto str4=str3;
for (int i = 0; i < allocate_length; i++)
{
str_allocate.construct(str3++,"Hello World");
}
str_allocate.deallocate(str4,allocate_length);
end = clock();
std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl;
return 0;
}