c++ - 对几乎相同的代码执行不同的向量求和
问题描述
g++
使用flag编译时,这些函数对数组和向量求和似乎存在性能差异-O3
:
float sum1(float* v, int length) {
float sum = 0;
for(int i = 0; i < length; i++) {
sum += v[i];
}
return sum;
}
float sum2(std::vector<float> v) {
return sum1(&v[0], v.size());
}
sum1
例如,当使用长度为 100000 且sum2
使用具有相同长度和内容的向量进行调用时,sum2
最终结果大约为 100000。比sum1
我的测试慢 10%。测量的运行时间是:
sum1: 0.279816 ms
sum2: 0.307811 ms
现在这个开销是从哪里来的?附加您还可以找到我在那里犯错的可能性的完整测试代码。
[更新]通过引用 ( float sum2(std::vector<float>& v)
) 调用时,性能差异约为。还剩 3.7%,所以这有帮助,但是其他地方仍然有一些性能损失?
[Update2]其余部分似乎在统计上占主导地位,如更多迭代所示。因此,真正的唯一问题是通过引用调用!
完整的测试代码(用标志编译,也用 测试-O3
):g++
clang++
#include <iostream>
#include <chrono>
#include <vector>
using namespace std;
std::vector<float> fill_vector(int length) {
std::vector<float> ret;
for(int i = 0; i < length; i++) {
float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
ret.push_back(r);
}
return ret;
}
float sum1(float* v, int length) {
float sum = 0;
for(int i = 0; i < length; i++) {
sum += v[i];
}
return sum;
}
float sum2(std::vector<float> v) {
return sum1(&v[0], v.size());
}
int main() {
int iterations = 10000;
int vector_size = 100000;
srand(42);
std::vector<float> v1 = fill_vector(vector_size);
float* v2;
v2 = &v1[0];
std::chrono::duration<double, std::milli> duration_sum1(0);
for(int i = 0; i < iterations; i++) {
auto t1 = std::chrono::high_resolution_clock::now();
float res = sum1(v2, vector_size);
auto t2 = std::chrono::high_resolution_clock::now();
cout << "Result sum1: " << res << endl;
duration_sum1 += t2 - t1;
}
duration_sum1 /= iterations;
std::chrono::duration<double, std::milli> duration_sum2(0);
for(int i = 0; i < iterations; i++) {
auto t1 = std::chrono::high_resolution_clock::now();
float res = sum2(v1);
auto t2 = std::chrono::high_resolution_clock::now();
cout << "Result sum2: " << res << endl;
duration_sum2 += t2 - t1;
}
duration_sum2 /= iterations;
cout << "Durations:" << endl;
cout << "sum1: " << duration_sum1.count() << " ms" << endl;
cout << "sum2: " << duration_sum2.count() << " ms" << endl;
}
解决方案
我认为开销来自传递向量。
尝试传递参考:
float sum2(std::vector<float>& v)
推荐阅读
- php - Add to cart validation based on product attribute in WooCommerce
- flutter - 我的 flutter_bloc 演示不起作用。脚手架的身体似乎全部变灰了。我在这里想念什么?
- android - 对应用于 Glide 的占位符做些什么?
- java - 页面刷新时的 Spring 安全 web.xml 会话
- css - 使用 SVG 时文本消失
- javascript - 无法读取长度属性
- libpq - 如何为 PQexecParams 的 paramValues 创建类型 '[*c]const [*c]const u8'
- python - Numpy 数组的打印方式不同
- string - 以 api/ 格式搜索列表 url 搜索 api
- c++ - 为什么地图值不更新?