This study explores running times of different ways to program cellular automata in C and C++, i.e. looping through arrays by different means, the effect of structures and objects, and the choice of data structure (array versus vector in C++) and compiler (GNU gcc versus Apple clang). Using arrays instead of vectors, using pointers instead of array indices, using C instead of C++, and using structures and objects instead of primitive data types has little to negligible effects on the running time. Also, the choice of compiler has only a minor effect, except for simple update functions, in which case clang seems to find better ways to optimise it, especially for index-based access. If one is interested in multi-state cellular automata, structures and objects can be used without loss of performance in C and C++, respectively. The study supports the recommendation from practice to use vector in C++ and adds to it the use of index-based access for cellular automata. Future studies might investigate Apple's Metal shader or compiler optimisation, especially with respect to the update function.
翻译:本研究探讨了在C和C++中实现元胞自动机的不同编程方式的运行时间,包括通过不同方式遍历数组、结构和对象的影响、数据结构的选择(C++中的数组与向量)以及编译器(GNU gcc与Apple clang)的选择。使用数组而非向量、使用指针而非数组索引、使用C而非C++、以及使用结构和对象而非原始数据类型对运行时间的影响微乎其微。此外,编译器的选择影响较小,但对于简单的更新函数,clang似乎能找到更好的优化方式,特别是在基于索引的访问中。如果关注多状态元胞自动机,可以分别在C和C++中使用结构和对象而不会损失性能。本研究支持实践中使用C++向量的建议,并补充了在元胞自动机中使用基于索引的访问方式。未来的研究可能会探讨Apple的Metal着色器或编译器优化,特别是关于更新函数方面。