MemoryCounter
众所周知,在你定义的所有全局变量前和后分别加上这两行然后输出两个变量地址差值就可以获得你的程序所使用的静态内存。
bool START;
bool END;
cerr<<((&START)-(&END))/1024.;
但是哥们觉得每次都要写这么几行太麻烦了,浪费了我好几秒的时间。
所以哥们就写出了这个。
MemoryCounter V1.16
注意:这个程序只适合我的码风,对于不同人的码风程序需要稍作修改。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000;
const int MaxMB=512;
const bool Div=true;
ll instruct,structsum[N];
bool havell[N];
map<char,ll> constnumber;
char file_name[N];
string inp;
void str_to_ch(string str,char ch[]){
for(int i=0;i<str.size();i++){
ch[i]=str[i];
}
}
ll get_base(ll id){
if(inp[id+1]=='*'){
return constnumber[inp[id]]*constnumber[inp[id+2]];
}
if(inp[id+1]=='<'){
return constnumber[inp[id]]<<(inp[id+3]-'0');
}
if(inp[id+1]=='>'){
return constnumber[inp[id]]>>(inp[id+3]-'0');
}
return constnumber[inp[id]];
}
ll work1(ll startid,ll base,bool op){
ll id=startid,now=base,res=0;
while(inp[id]!=';'){
if(inp[id]=='['){
now*=get_base(id+1);
res+=56;
}
if(inp[id]==','){
res+=now;
now=base;
}
id++;
}
if(op){
return max(8ll,now+res);
}else{
return now+res;
}
}
ll work2(ll singlebase,ll multiplebase,ll other){
ll id=inp.find(' ')+1,now=singlebase,res=0;
bool flag=false;
while(inp[id]!=';'){
if(inp[id]=='['){
now=get_base(id+1)*multiplebase;
flag=true;
}
if(inp[id]==','){
res+=now;
now=singlebase;
}
id++;
}
return now+res+(flag?56:other);
}
ll work3(){
ll id=inp.find(' ')+1,base=((constnumber[inp[id-3]]-1)/32+1)*4;
ll now=base,res=0;
while(inp[id]!=';'){
if(inp[id]=='['){
now*=get_base(id+1);
}
if(inp[id]==','){
res+=now+48;
now=base;
}
id++;
}
return now+res+56;
}
ll work4(ll base){
ll id=1,now=base,res=0;
bool flag=false;
while(inp[id]!=';'){
if(inp[id]=='['){
now*=get_base(id+1);
flag=true;
}
if(inp[id]==','){
res+=now;
now=base;
}
id++;
}
return now+res+(flag?56:0);
}
ll solvenowline(bool op){
if(inp[0]=='l'&&inp[1]=='l'){
havell[instruct]=true;
return work1(3,8,false);
}
if(inp[0]=='b'&&inp[1]=='o'&&inp[2]=='o'&&inp[3]=='l'){
return havell[instruct]?work1(5,1,true):work1(5,1,false);
}
if(inp[0]=='s'&&inp[1]=='t'&&inp[2]=='r'&&inp[3]=='i'&&inp[4]=='n'&&inp[5]=='g'){
havell[instruct]=true;
return work1(7,8,false);
}
if(inp[0]=='c'&&inp[1]=='h'&&inp[2]=='a'&&inp[3]=='r'){
return havell[instruct]?work1(5,1,true):work1(5,1,false);
}
if(inp[0]=='d'&&inp[1]=='o'&&inp[2]=='u'){
return work1(7,8,false);
}
if(inp[0]=='l'&&inp[1]=='o'&&inp[2]=='n'){
return work1(12,16,false);
}
if(inp[0]=='p'&&inp[1]=='i'&&inp[2]=='r'){
return work1(4,16,false);
}
if(inp[0]=='v'&&inp[1]=='e'&&inp[2]=='c'){
return work2(32,24,0);
}
if(inp[0]=='q'&&inp[1]=='u'&&inp[2]=='e'){
return work2(128,80,8);
}
if(inp[0]=='d'&&inp[1]=='e'&&inp[2]=='q'){
return work2(128,80,8);
}
if(inp[0]=='s'&&inp[1]=='e'&&inp[2]=='t'){
return work2(64,48,8);
}
if(inp[0]=='m'&&inp[1]=='a'&&inp[2]=='p'){
return work2(64,48,8);
}
if(inp[0]=='p'&&inp[1]=='r'&&inp[2]=='i'&&inp[3]=='o'){
return work2(32,32,24);
}
if(inp[0]=='b'&&inp[1]=='i'&&inp[2]=='t'){
return work3();
}
return 0ll;
}
int main(){
getline(cin,inp);
str_to_ch(inp,file_name);
freopen(file_name,"r",stdin);
while(getline(cin,inp)){
if(inp[0]=='c'&&inp[1]=='o'&&inp[2]=='n'&&inp[3]=='s'&&inp[4]=='t'){
if(inp[6]!='i'||inp[7]!='n'||inp[8]!='t'){
continue;
}
if(inp[10]=='i'&&inp[11]=='n'&&inp[12]=='f'){
continue;
}
if(inp[10]=='M'&&inp[11]=='O'&&inp[12]=='D'){
continue;
}
char constname=inp[10];
ll nowconstnumber=0,startid=12;
if(inp[13]=='<'||inp[13]=='>'){
startid=15;
}
for(int i=startid;inp[i]!=';';i++){
nowconstnumber=(nowconstnumber*10+inp[i]-'0');
}
if(inp[13]=='<'){
nowconstnumber=(inp[12]-'0')<<nowconstnumber;
}
if(inp[13]=='>'){
nowconstnumber=(inp[12]-'0')>>nowconstnumber;
}
constnumber[constname]=nowconstnumber;
}
if(instruct){
inp.erase(0,instruct-1);
if(inp[0]=='}'&&inp[inp.size()-1]==';'){
instruct--;
structsum[instruct]+=work4(structsum[instruct+1]);
structsum[instruct+1]=0;
havell[instruct+1]=false;
}else{
inp.erase(0,1);
}
}
if(inp[0]=='s'&&inp[1]=='t'&&inp[2]=='r'&&inp[3]=='u'){
instruct++;
}
if(inp[inp.size()-1]=='{'){
continue;
}
structsum[instruct]+=solvenowline(instruct);
}
cout<<"/*\n";
if(Div){
cout<<"Max: "<<MaxMB*1024<<" kB\n";
cout<<"Use: "<<structsum[0]/1024.<<" kB\n";
}else{
cout<<"Max: "<<MaxMB*1024*1024<<" B\n";
cout<<"Use: "<<structsum[0]<<" B\n";
}
cout<<"*/";
}
更新日志:
V0.8
试用版发布,当前缺失计算 struct 静态空间模块。
V0.98
正式版发布,可以计算 struct 静态空间,输出包含更详细的信息。
V1.02
添加了数组大小包含运算符情况的处理,输出包含了完整可复制粘贴代码,并且可以方便调整最大空间输出。
V1.06
添加了在常量中进行表达式求值的计算,现在常量中可以进行左移右移等操作。
V1.10
修正了关于同域内各类型容器内存处理问题,现在可以正确处理 ll,bool 这些单个存储空间不同但是被玄学负优化成相同空间的情况。
同时增加可调节输出空间单位的功能。
V1.16
补上了 double 和 long double 空间的计算。