代码拉取完成,页面将自动刷新
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include<string>
#include <assert.h>
#include<map>
#include "List_Node.h"
#include "MemoryPool.h"
#include "CurrAlloc.h"
#include<time.h>
#include <thread>
#include <mutex>
#include<iostream>
using namespace std;
/**
* Memory Leak Caution! Release memory manually.
*/
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
void BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
std::vector<std::thread> vthread(nworks);
std::atomic<size_t> malloc_costtime = 0;
std::atomic<size_t> free_costtime = 0;
//size_t malloc_costtime = 0;
//size_t free_costtime = 0;
for (size_t k = 0; k < nworks; ++k)
{
vthread[k] = std::thread([&, k]() {
std::vector<void*> v;
v.reserve(ntimes);
for (size_t j = 0; j < rounds; ++j)
{
size_t begin1 = clock();
for (size_t i = 0; i < ntimes; i++)
{
v.push_back(malloc(16 + i * 16));
//v.push_back(malloc((16 + i) % 8192 + 1));
}
size_t end1 = clock();
size_t begin2 = clock();
for (size_t i = 0; i < ntimes; i++)
{
free(v[i]);
}
size_t end2 = clock();
v.clear();
malloc_costtime += (end1 - begin1);
free_costtime += (end2 - begin2);
}
});
}
for (auto& t : vthread)
{
t.join();
}
cout << nworks << "个线程并发执行" << rounds << "轮次,每轮次malloc " << ntimes << "次: 花费:" << (unsigned int)malloc_costtime << endl;
cout << nworks << "个线程并发执行" << rounds << "轮次,每轮次free " << ntimes << "次: 花费:" << (unsigned int)free_costtime << endl;
cout << nworks << "个线程并发malloc&free" << nworks * rounds * ntimes << "次,总计花费:"<< (unsigned int)malloc_costtime + (unsigned int)free_costtime << endl;
}
// 单轮次申请释放次数 线程数 轮次
void BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
std::vector<std::thread> vthread1(nworks);
std::atomic<size_t> malloc_costtime1 = 0;
std::atomic<size_t> free_costtime1 = 0;
/*size_t malloc_costtime = 0;
size_t free_costtime = 0;*/
for (size_t k = 0; k < nworks; ++k)
{
vthread1[k] = std::thread([&]() {
std::vector<void*> v;
v.reserve(ntimes);
for (size_t j = 0; j < rounds; ++j)
{
size_t begin1 = clock();
for (size_t i = 0; i < ntimes; i++)
{
v.push_back(ConcurrentAlloc(16 + i * 16));
//v.push_back(ConcurrentAlloc((16 + i) % 8192 + 1));
}
size_t end1 = clock();
size_t begin2 = clock();
for (size_t i = 0; i < ntimes; i++)
{
//ConcurrentFree(v[i]);
}
size_t end2 = clock();
v.clear();
malloc_costtime1 += (end1 - begin1);
free_costtime1 += (end2 - begin2);
}
});
}
for (auto& t : vthread1)
{
t.join();
}
cout << nworks << "个线程并发执行" << rounds << "轮次,每轮次concurrent alloc " << ntimes << "次: 花费:" << (unsigned int)malloc_costtime1 << endl;
cout << nworks << "个线程并发执行" << rounds << "轮次,每轮次concurrent dealloc " << ntimes << "次: 花费:" << (unsigned int)free_costtime1 << endl;
cout << nworks << "个线程并发concurrent alloc&dealloc" << nworks * rounds * ntimes << "次,总计花费:" << (unsigned int)free_costtime1 + (unsigned int)malloc_costtime1 << endl;
}
class TestMemoryPool
{
private:
int value1 = 10;
int value2 = 20;
float val = 5.9f;
public:
TestMemoryPool() {}
static void* operator new(size_t)
{
return ConcurrentAlloc(sizeof(TestMemoryPool));
}
static void operator delete(void* p) {
ConcurrentFree(p);
}
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(159);
cout << "==========================================================" << endl;
////123
TestMemoryPool* p = new TestMemoryPool();
//BenchmarkMalloc(100, 30, 10);
//cout << endl;
//BenchmarkConcurrentMalloc(100, 30, 10);
//cout << endl << endl;
cout << "==========================================================" << endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。