c++ - 我将如何优化此代码?
问题描述
我正在编写一个函数来查找数组的平均值,其中数组主要是如果一次全部添加会溢出的数字。
它的工作原理是创建一个子数组(在我的代码中),它是输入(在我的代码中)数组大小(在我的代码中)b
的一半,然后将输入数组中的 2 个值的平均值放入.a
ar_size
a[i+0] and a[i+1]
b[j]
一旦遍历整个输入数组,它就会重新运行函数并返回子数组和输入数组的大小,直到大小等于 2,然后通过返回 的两个值的平均值来结束递归b[2]
。
请原谅重复使用j
.
数组的大小也是二的幂。
uint64_t* array_average(uint64_t* a, const int ar_size)
{
uint64_t* b = new uint64_t[ar_size / 2];
uint64_t* j = new uint64_t;
if (ar_size == 2)
{
*j = (a[0] / 2) + (a[1] / 2) + ((a[0] % 2 + a[1] % 2) / 2);
return j;
}
for (int i = 0; i < ar_size; i += 2)
{
b[*j] = (a[i + 0] / 2) + (a[i + 1] / 2) + ((a[i + 0] % 2 + a[i + 1] % 2) / 2);
++*j;
}
delete j;
return array_average(b, ar_size / 2);
}
还有人在处理会导致溢出的数字时有更好的平均方法吗?
这是一个修订版:
uint64_t* tools::array_average(uint64_t* a, const int ar_size)
{
uint64_t* b = new uint64_t[ar_size];
uint64_t* c = new uint64_t[ar_size / 2];
int j;
j = 0;
for (int i = 0; i < ar_size; ++i)
{
b[i] = a[i];
}
if (runs > 0) //This is so i do not delete the original input array I.E not done with it
{
delete[] a;
}
if (ar_size == 2)
{
uint64_t* y = new uint64_t;
runs = 0;
*y = (b[0] / 2) + (b[1] / 2) + ((b[0] % 2 + b[1] % 2) / 2);
delete[] b;
return y;
}
for (int i = 0; i < ar_size; i += 2)
{
c[j] = (b[i + 0] / 2) + (b[i + 1] / 2) + ((b[i + 0] % 2 + b[i + 1] % 2) / 2);
++j;
}
delete[] b;
++runs;
return array_average(c, ar_size / 2);
解决方案
首先,请注意您的平均值不是实际平均值,因为您确实扔掉了一半。您的算法在 0 和 1 之间交替的数组上的结果将是 0,即 0/2 + 1/2 + (0%2 + 1%2)/2 = 0。想从那个开始,因为那是您的算法的严重弱点。
另请注意,如果原始大小不是 2 的幂,则某些数据将获得更高的权重。
除此之外,考虑这个算法:复制数据。直到数据只剩下一个条目,将单元格 0 和 1 的平均值放在单元格 0 中,将 2 和 3 的平均值放在单元格 1 中,将 4 和 5 的平均值放在 2 中,依此类推。在每个这样的步骤之后收缩数据。
作为代码:
uint64_t average(std::vector<uint64_t> data)
{
while(data.size() != 1)
{
for(size_t i=0; i<data.size()/2; i++)
{
data[i] = data[2*i]/2 + data[2*i+1]/2 + /* modular stuff */;
}
data.resize(data.size()/2 + data.size()%2); //last part is required if the size is not an even number
}
return data[0];
}
顺便说一句,在这里使用合适的容器也可以消除内存泄漏。
请注意,这段代码仍然有我谈到的弱点。您可以通过收集一半来扩展它,也就是说,如果您的模块化部分为 1,则增加一个变量,当变量为 2 时,您在某个单元格中添加一个。
编辑:如果输入必须是原始数组(例如,因为您从某个外部源接收它),请使用以下命令:
uint64_t average(uint64_t* array, const int array_size)
{
std::vector<uint64_t> data(array, array + array_size);
(rest of the code is identical)
编辑:上面的代码收集了一半:
inline uint64_t average(const uint64_t& a, const uint64_t& b, uint8_t& left_halves)
{
uint64_t value = a/2 + b/2 + (a%2 + b%2)/2;
if((a%2 + b%2)%2 == 1)
{
left_halves += 1;
}
if(left_halves == 2)
{
value += 1;
left_halves = 0;
}
return value;
}
uint64_t average(std::vector<uint64_t> data)
{
if(data.size() == 0) return 0;
uint8_t left_halves = 0;
while(data.size() != 1)
{
for(size_t i=0; i<data.size()/2; i++)
{
data[i] = average(data[2*i], data[2*i+1], left_halves);
}
data.resize(data.size()/2 + data.size()%2); //last part is required if the size is not an even number
}
return data[0];
}
如果尺寸不是 2 的幂,则仍有增加单元重量的弱点。
推荐阅读
- azure - Azure CLI - 将传入客户端证书设置为允许 Web 应用程序
- python - 删除数据框索引上的粗体格式
- java - 如何正确解开 Java 中的可选项?
- excel - 预期:Sheets("PrepaymentCustomer").Range("B2:B4") 的语句结束 (VBA)
- youtube-api - 如何授予管理其他成员进入 YouTube API 项目的权限
- javascript - 在文件流和 cvparser 解析数据的问题
- java - 创建具有正确权限的 CSV 文件
- css - 当模态在本机反应中打开时,我想模糊背景
- php - Elementor 添加新的自定义响应断点
- python - OpenCV:使用 Canny 和 Shi-Tomasi 检测扑克牌的圆角