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). The choice of data structure influenced the running time the most. The array version is more than 20-times faster than the vector version in C++. The choice of compiler also had an effect, with the GNU gcc compiler delivering 1.7-times faster programs as compared to the Apple clang compiler. Using pointers instead of array indices, using C instead of C++, and using structures and objects instead of primitive data types has little to neglectable effects on the running time. The study shows that using arrays and looping over them by index in C++ and compiled with GNU gcc reveals the best performance with respect to running time. If one is interested in multi-state cellular automata, objects can be used without loss of that performance. Future studies might investigate Apple's Metal shader or compiler optimisation.
翻译:本研究探讨了在C和C++中编程实现元胞自动机的不同方法的运行时间,包括通过不同方式遍历数组、结构和对象的影响、数据结构的选择(C++中的数组与向量)以及编译器(GNU gcc与Apple clang)的选择。数据结构的选择对运行时间影响最大。在C++中,数组版本比向量版本快20倍以上。编译器的选择也有影响,GNU gcc编译器生成的程序比Apple clang编译器快1.7倍。使用指针而非数组索引、使用C而非C++、以及使用结构和对象而非原始数据类型对运行时间的影响微乎其微。研究表明,在C++中使用数组并通过索引遍历,且使用GNU gcc编译,可获得最佳的运行时间性能。若关注多状态元胞自动机,使用对象不会损失该性能。未来研究可探讨Apple的Metal着色器或编译器优化。