计算一部分算法消耗的空间 [英] compute space consumed by a portion of algorithm

查看:61
本文介绍了计算一部分算法消耗的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定C/C ++中的程序占用的空间?有内置的库可以找到它吗?

例如,x将消耗多少字节?其中x是某种复杂的函数.

how to identify the space consumed by a program in C/C++? is there any built in library to find it?

for example, how many bytes will be consumed by x? where x is somehow complex function.

int main()
{
    int t= x();
    printf("%d",t);
}

推荐答案

如果运行时不需要该信息,则可以从链接程序创建的地图文本文件中获取该信息(例如,使用命令行选项/MAP和MS链接器).使用文本编辑器打开地图文件,然后搜索感兴趣的功能.可以通过从函数的地址中减去以下函数的地址来计算大小.

在运行时需要大小时,可以将结果作为常量值添加到程序中.
If the information is not required during run-time, you may get it from a map text file created by the linker (e.g. use command line option /MAP with the MS linker). Open the map file with a text editor and search for the functions of interest. The size can be calculated by subtracting the address of the following function from the address of your function.

When you need the sizes during run-time, you may add the results as constant values to your program.




在运行时,您可以使用函数指针来计算函数指令消耗的内存".

Hi,

At runtime you could use function pointers to calculate ''memory consumed by the instructions of a function''.

#pragma optimize( "", off )
void __declspec(naked) MeasureMe()
{
	__asm nop;
}

int end_of_measurement()
{
	return 42; //The true meaning of life;
}

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned char * start_offset = (unsigned char *)&MeasureMe;
	printf("%u\n",start_offset);
	unsigned char * end_offset = (unsigned char *)&end_of_measurement;
	printf("%u\n",end_offset);
	printf("%u\n",end_offset - start_offset);
	//Release mode should return 1 * number of NOP instructions... + length of prologue and length of epilogue
	//void __declspec(naked) functions will not have a prologue or an epilogue
	//Also...keep in mind that the return value may be 4-byte aligned on 32 bit processor and 16-byte aligned on x64.
}



确保在发布模式"下进行测试.

最好的祝福,
-大卫·德劳恩(David Delaune)



Make sure that you test under ''Release Mode''.

Best Wishes,
-David Delaune


这篇关于计算一部分算法消耗的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆