Python 在C+中运行算法内存使用实验的好方法是什么+;? 我在C++中实现了算法 A算法>代码>代码>代码>。从理论上讲,code>A比B使用更多的空间,事实证明在实践中也是如此。我想生成一些漂亮的图表来说明这一点。两种算法都会收到一个输入,我希望我的实验会因不同的n而有所不同,因此图形的x轴必须类似n=10^6,2*10^6,…
通常,对于时间或缓存未命中等数据,我最喜欢的实验设置方法如下。在C++文件中,我有这样的算法:Python 在C+中运行算法内存使用实验的好方法是什么+;? 我在C++中实现了算法 A算法>代码>代码>代码>。从理论上讲,code>A比B使用更多的空间,事实证明在实践中也是如此。我想生成一些漂亮的图表来说明这一点。两种算法都会收到一个输入,我希望我的实验会因不同的n而有所不同,因此图形的x轴必须类似n=10^6,2*10^6,…,python,c++,algorithm,valgrind,Python,C++,Algorithm,Valgrind,通常,对于时间或缓存未命中等数据,我最喜欢的实验设置方法如下。在C++文件中,我有这样的算法: #include <iostream> using namespace std; int counters[1000]; void init_statistics(){ //use some library for example papi (http://icl.cs.utk.edu/papi/software/) //to start counting, store the
#include <iostream>
using namespace std;
int counters[1000];
void init_statistics(){
//use some library for example papi (http://icl.cs.utk.edu/papi/software/)
//to start counting, store the results in the counters array
}
void stop_statistics(){
//this is just to stop counting
}
int algA(int n){
//algorithm code
int result = ...
return result;
}
void main(int argc, const char * argv[]){
int n = atoi(argv[1]);
init_statistics(); //function that initializes the statistic counters
int res = algA(n);
end_statistics(); //function that ends the statistics counters
cout<<res<<counter[0]<<counter[1]<<....<<endl;
}
有趣的数字是471174306字节
。然而,valgrind大大降低了执行时间,同时不仅返回这个数字,还返回这个大字符串。我不知道如何解析它,因为出于某种原因,如果用python调用result=subprocess.check_output(['valgrind','./algB',…])
,则result
字符串只存储/algB
的输出,而完全忽略valgrind返回的内容
非常感谢你
memcheck
是一种查找内存泄漏的工具,您应该使用它进行内存分配分析。您应该能够如示例所示进行精确测量。这似乎是一个不错的选择,但是否有方法返回内存峰值消耗?仅返回内存峰值消耗?这就是任何流程监控实用程序都可以向您展示的内容。如果您使用的是VS,那么您也可以使用内置内存监视器(主菜单->分析->性能分析器)。谢谢,我会记住这一点。我试过massif,老实说,尽管它速度很慢,但它给出了非常详细的内存消耗概述。在考虑了您在评论中所写的内容之后,我想我必须使用/usr/bin/time-v
和它所具有的最大驻留集大小(KB)
变量。。。
==15447== Memcheck, a memory error detector
==15447== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15447== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15447== Command: ./algB 1.txt 2.txt
==15447==
==15447==
==15447== HEAP SUMMARY:
==15447== in use at exit: 72,704 bytes in 1 blocks
==15447== total heap usage: 39 allocs, 38 frees, 471,174,306 bytes allocated
==15447==
==15447== LEAK SUMMARY:
==15447== definitely lost: 0 bytes in 0 blocks
==15447== indirectly lost: 0 bytes in 0 blocks
==15447== possibly lost: 0 bytes in 0 blocks
==15447== still reachable: 72,704 bytes in 1 blocks
==15447== suppressed: 0 bytes in 0 blocks
==15447== Rerun with --leak-check=full to see details of leaked memory
==15447==
==15447== For counts of detected and suppressed errors, rerun with: -v
==15447== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)