世纪问题?我基本上想知道如果我将此代码编写为几个不同的变量或使用小数组,哪个会更有效。
int x = 34;
int y = 28;
int z = 293;
vs
double coordinate[3] = {34, 28, 293};
我有一个坐标结构,我将按以下方式使用它:
typedef struct coordinates_t {
double x = 0.0;
double y = 0.0;
double z = 0.0;
} coordinates;
typedef struct car_t {
coordinates start; // car starting point
coordinates location; // car current Location
coordinates targCarVector; // Vector to car from target
coordinates altitude; // Altitude of car
coordinates distance; // Distance from car start to current position
} car;
我需要做类似的事情:
distance = car1.location - car1.start;
如果我不使用数组,我将不得不有很多行代码,但如果我使用数组,我将不得不使用循环。数组和循环是否更占用内存/CPU?我基本上想看看哪种是编写这段代码最有效的方法。
谢谢,
半羊
效率比可维护性和可读性更重要吗?答案是不。即使您有一个对时间要求严格的应用程序,您也会将 90% 的时间花在不到 10% 的代码上,因此只需尽可能高效地编码这 10% 的代码。
如果您还没有测量并发现哪 10% 是罪魁祸首,那么您几乎肯定会优化一开始就不需要太多运行时间的代码。这是浪费时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)