cocos2d 文件系统使用文件内存映射性能对比
//cocos 修改代码
.....
//性能测试代码
extern "C" {
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
void ptest(){
auto TimeDiff = [](std::function<void()> func,const char* msg){
clock_t t1 , t2;
t1 = clock();
func();
t2 = clock();
CCLOG(msg,t2-t1);
};
auto writef = [](const char* path,int len){
FILE* fp = fopen(path, "wr+");
unsigned char* c = (unsigned char*)malloc(len);
memset(c, 'T', len);
fwrite(c, 1, len, fp);
fflush(fp);
fclose(fp);
fp =nullptr;
};
auto readfile = [](const char* path){
std::string src("src/ptest/datas/");
src.append(path);
FileUtils::getInstance()->getStringFromFile(src);
};
auto readfilea = [](const char* path){
std::string src(FileUtils::getInstance()->getWritablePath());
src.append("/");
src.append(path);
FileUtils::getInstance()->getStringFromFile(src);
};
std::string writepath = FileUtils::getInstance()->getWritablePath();
auto fullpath = [](std::string path,const char* str){ path.append(str); return path; };
writef(fullpath(writepath,"128B.s").c_str(),128);
writef(fullpath(writepath,"512B.s").c_str(),512);
writef(fullpath(writepath,"2K.s").c_str(),2*1024);
writef(fullpath(writepath,"4K.s").c_str(),4*1024);
writef(fullpath(writepath,"16K.s").c_str(),16*1024);
writef(fullpath(writepath,"512K.s").c_str(),512*1024);
writef(fullpath(writepath,"1M.s").c_str(),1*1024*1024);
writef(fullpath(writepath,"2M.s").c_str(),2*1024*1024);
writef(fullpath(writepath,"5M.s").c_str(),5*1024*1024);
writef(fullpath(writepath,"10M.s").c_str(),10*1024*1024);
writef(fullpath(writepath,"20M.s").c_str(),20*1024*1024);
//read
TimeDiff([&](){ readfile("128B.s"); },"assert 128 bytes time:%d");
TimeDiff([&](){ readfilea("128B.s"); },"Writable 128 bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("512B.s"); },"assert 512 bytes time:%d");
TimeDiff([&](){ readfilea("512B.s"); },"Writable 512 bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("2K.s"); },"assert 2k bytes time:%d");
TimeDiff([&](){ readfilea("2K.s"); },"Writable 2k bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("4K.s"); },"assert 4k bytes time:%d");
TimeDiff([&](){ readfilea("4K.s"); },"Writable 4k bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("16K.s"); },"assert 16k bytes time:%d");
TimeDiff([&](){ readfilea("16K.s"); },"Writable 16k bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("512K.s"); },"assert 512K bytes time:%d");
TimeDiff([&](){ readfilea("512K.s"); },"Writable 512K bytes time:%d"); CCLOG("");
TimeDiff([&](){ readfile("1M.s"); },"assert 1M.s bytes time:%d");
TimeDiff([&](){ readfilea("1M.s"); },"Writable 1M.s time:%d"); CCLOG("");
TimeDiff([&](){ readfile("2M.s"); },"assert 2M.s bytes time:%d");
TimeDiff([&](){ readfilea("2M.s"); },"Writable 512K time:%d"); CCLOG("");
TimeDiff([&](){ readfile("5M.s"); },"assert 5M.s bytes time:%d");
TimeDiff([&](){ readfilea("5M.s"); },"Writable 5M.s time:%d"); CCLOG("");
TimeDiff([&](){ readfile("10M.s"); },"assert 10M.s bytes time:%d");
TimeDiff([&](){ readfilea("10M.s"); },"Writable 10M.s time:%d"); CCLOG("");
TimeDiff([&](){ readfile("20M.s"); },"assert 20M.s bytes time:%d");
TimeDiff([&](){ readfilea("20M.s"); },"Writable 20M.s time:%d"); CCLOG("");
}
}
结果对比:
assert 128 bytes time:1951
Writable 128 bytes time:544
assert 512 bytes time:939
Writable 512 bytes time:0
assert 2k bytes time:1153
Writable 2k bytes time:374
assert 4k bytes time:883
Writable 4k bytes time:0
assert 16k bytes time:1244
Writable 16k bytes time:0
assert 512K bytes time:3366
Writable 512K bytes time:1958
assert 1M.s bytes time:4856
Writable 1M.s time:2206
assert 2M.s bytes time:12929
Writable 512K time:4581
assert 5M.s bytes time:27459
Writable 5M.s time:20102
assert 10M.s bytes time:38956
Writable 10M.s time:24224
//=====================================
cocos2d 测试代码
//#ifdef s
////内存映射测试
//extern "C" {
//#include <time.h>
//#include <stdlib.h>
//#include <stdio.h>
// void ptest(){
// auto TimeDiff = [](std::function<void()> func,const char* msg){
// clock_t t1 , t2;
// t1 = clock();
// func();
// t2 = clock();
// CCLOG(msg,t2-t1);
// };
//
// auto writef = [](const char* path,int len){
// FILE* fp = fopen(path, "wr+");
// unsigned char* c = (unsigned char*)malloc(len);
// memset(c, 'T', len);
// fwrite(c, 1, len, fp);
// fflush(fp);
// fclose(fp);
// fp =nullptr;
// };
//
// auto readfile = [](const char* path){
// std::string src("src/ptest/datas/");
// src.append(path);
// FileUtils::getInstance()->getStringFromFile(src);
// };
//
// auto readfilea = [](const char* path){
// std::string src(FileUtils::getInstance()->getWritablePath());
// src.append("/");
// src.append(path);
// FileUtils::getInstance()->getStringFromFile(src);
// };
//
//
// std::string writepath = FileUtils::getInstance()->getWritablePath();
//
// auto fullpath = [](std::string path,const char* str){ path.append(str); return path; };
//
// writef(fullpath(writepath,"128B.s").c_str(),128);
// writef(fullpath(writepath,"512B.s").c_str(),512);
// writef(fullpath(writepath,"2K.s").c_str(),2*1024);
// writef(fullpath(writepath,"4K.s").c_str(),4*1024);
// writef(fullpath(writepath,"16K.s").c_str(),16*1024);
// writef(fullpath(writepath,"512K.s").c_str(),512*1024);
// writef(fullpath(writepath,"1M.s").c_str(),1*1024*1024);
// writef(fullpath(writepath,"2M.s").c_str(),2*1024*1024);
// writef(fullpath(writepath,"5M.s").c_str(),5*1024*1024);
// writef(fullpath(writepath,"10M.s").c_str(),10*1024*1024);
// writef(fullpath(writepath,"20M.s").c_str(),20*1024*1024);
//
// //read
// TimeDiff([&](){ readfile("128B.s"); },"assert 128 bytes time:%d");
// TimeDiff([&](){ readfilea("128B.s"); },"Writable 128 bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("512B.s"); },"assert 512 bytes time:%d");
// TimeDiff([&](){ readfilea("512B.s"); },"Writable 512 bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("2K.s"); },"assert 2k bytes time:%d");
// TimeDiff([&](){ readfilea("2K.s"); },"Writable 2k bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("4K.s"); },"assert 4k bytes time:%d");
// TimeDiff([&](){ readfilea("4K.s"); },"Writable 4k bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("16K.s"); },"assert 16k bytes time:%d");
// TimeDiff([&](){ readfilea("16K.s"); },"Writable 16k bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("512K.s"); },"assert 512K bytes time:%d");
// TimeDiff([&](){ readfilea("512K.s"); },"Writable 512K bytes time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("1M.s"); },"assert 1M.s bytes time:%d");
// TimeDiff([&](){ readfilea("1M.s"); },"Writable 1M.s time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("2M.s"); },"assert 2M.s bytes time:%d");
// TimeDiff([&](){ readfilea("2M.s"); },"Writable 2M.s time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("5M.s"); },"assert 5M.s bytes time:%d");
// TimeDiff([&](){ readfilea("5M.s"); },"Writable 5M.s time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("10M.s"); },"assert 10M.s bytes time:%d");
// TimeDiff([&](){ readfilea("10M.s"); },"Writable 10M.s time:%d"); CCLOG("");
//
// TimeDiff([&](){ readfile("20M.s"); },"assert 20M.s bytes time:%d");
// TimeDiff([&](){ readfilea("20M.s"); },"Writable 20M.s time:%d"); CCLOG("");
//
// }
//}
//#endif
assert 20M.s bytes time:85986
Writable 20M.s time:50815
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构