我熟悉科学的流体动力学代码。代码几乎总是使用预处理程序指令,例如
#ifdef PARTICLES
int nghost = 5
#else
int nghost = 4
#endif
而不是更简单的C标志,比如,
int nghost = 4;
if( particlesFlag ) { nghost = 5; }
预处理程序标志的缺点是(在此框架中)它需要在每次构建之前为每个问题设置配置(使用头文件创建),其中使用c代码标志只需要重新编译。
这种方法的优点是什么?
似乎效率的任何提高都会非常小 - 特别是因为这个代码(例如)只在程序初始化时运行一次,而所有真正的劳动力都发生在不同处理器的循环中,等
答案 0 :(得分:0)
假设我使用nghost有数千个API。如果已经定义了粒子,那么在预处理过程中,所有这些变量将被4替换为4。
您正在谈论单个实例,一个大项目是预处理很有用的地方。
想一想。
int x() { int a = nghost *5; }
int ab() { return (nghost+10); }
每次使用时,其运行时消耗
int x() {
int a;
int nghost = 4;
if( particlesFlag ) { nghost = 5;}
a=nghost*5;
}
int ab() {
int nghost = 4;
if( particlesFlag ) { nghost = 5;}
return (nghost+10);
}
等等。
并考虑到这一点。
假设
#ifdef PARTICLES
int nghost = 5
int aghost = 5
int bghost = 5
int cghost = 5
int dghost = 5
int eghost = 5
int fghost = 5
#else
int nghost = 4
int aghost = 4
int bghost = 4
int cghost = 4
int dghost = 4
int eghost = 4
int fghost = 4
#endif