运行 实验 C++ 中算法的内存使用情况的好方法是什么?
what is a good way to run experiments for the memory usage of an algorithm in C++?
我有用 C++ 实现的算法 A
和算法 B
。 A
理论上比B
用的space多,实践证明也是这样。我想生成一些漂亮的图表来说明这一点。两种算法都收到一个输入 n
,我希望我的实验因不同的 n
而异,因此图表的 x 轴必须类似于 n = 10^6, 2*10^6, ...
通常,当涉及到时间或缓存未命中等数据时,我最喜欢的实验设置方式如下。在 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 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;
}
然后我会创建一个 python 脚本,用于不同的 n
调用 result = subprocess.check_output(['./algB',...])
。之后,解析python中的结果字符串,并以合适的格式打印出来。例如,如果我将 R 用于绘图,我可以将数据打印到外部文件,其中每个计数器由 \t
.
分隔
这对我来说效果很好,但现在是我第一次需要有关算法使用的 space 的数据,我不确定如何计算这个 space。一种方法是使用 valgrind,这是 valgrind 的可能输出:
==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)
有趣的数字是 471,174,306 bytes
。但是,valgrind 大大减慢了执行时间,同时不只是 return 这个数字,而是这个大字符串。而且我不确定如何解析它,因为出于某种原因,如果使用 python 我调用 result = subprocess.check_output(['valgrind','./algB',...])
,result
字符串仅存储 ./algB
的输出并完全忽略 valgrind returns.
提前谢谢你!
memcheck
是查找内存泄漏的工具,您应该使用 massif
(another tool available in valgrind) 进行内存分配分析。
我有用 C++ 实现的算法 A
和算法 B
。 A
理论上比B
用的space多,实践证明也是这样。我想生成一些漂亮的图表来说明这一点。两种算法都收到一个输入 n
,我希望我的实验因不同的 n
而异,因此图表的 x 轴必须类似于 n = 10^6, 2*10^6, ...
通常,当涉及到时间或缓存未命中等数据时,我最喜欢的实验设置方式如下。在 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 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;
}
然后我会创建一个 python 脚本,用于不同的 n
调用 result = subprocess.check_output(['./algB',...])
。之后,解析python中的结果字符串,并以合适的格式打印出来。例如,如果我将 R 用于绘图,我可以将数据打印到外部文件,其中每个计数器由 \t
.
这对我来说效果很好,但现在是我第一次需要有关算法使用的 space 的数据,我不确定如何计算这个 space。一种方法是使用 valgrind,这是 valgrind 的可能输出:
==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)
有趣的数字是 471,174,306 bytes
。但是,valgrind 大大减慢了执行时间,同时不只是 return 这个数字,而是这个大字符串。而且我不确定如何解析它,因为出于某种原因,如果使用 python 我调用 result = subprocess.check_output(['valgrind','./algB',...])
,result
字符串仅存储 ./algB
的输出并完全忽略 valgrind returns.
提前谢谢你!
memcheck
是查找内存泄漏的工具,您应该使用 massif
(another tool available in valgrind) 进行内存分配分析。