//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