- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下代码
#include <cstdlib>
#include <vector>
#include <chrono>
#include <iostream>
static const uint64_t BENCHMARK_RUNS(1000000);
std::vector<float> vec_mul_no_ref(const std::vector<float> x,
const std::vector<float> y) {
if(x.size() != y.size()) {
throw std::runtime_error("vectors are not the same size!");
}
std::vector<float> ans(x.size());
for (size_t ii=0; ii<x.size(); ii++) {
ans[ii] = x[ii] * y[ii];
}
return ans;
}
std::vector<float> vec_mul_ref_args(const std::vector<float>& x,
const std::vector<float>& y) {
if(x.size() != y.size()) {
throw std::runtime_error("vectors are not the same size!");
}
std::vector<float> ans(x.size());
for (size_t ii=0; ii<x.size(); ii++) {
ans[ii] = x[ii] * y[ii];
}
return ans;
}
void vec_mul_all_ref(const std::vector<float>& x,
const std::vector<float>& y, std::vector<float>& ans) {
if(x.size() != y.size() || y.size() != ans.size()) {
throw std::runtime_error("vectors are not the same size!");
}
for (size_t ii=0; ii<x.size(); ii++) {
ans[ii] = x[ii] * y[ii];
}
}
void bench_vec_mul() {
size_t vec_size(10000);
std::vector<float> x(vec_size);
std::vector<float> y(vec_size);
for(size_t ii=0; ii<vec_size; ii++) {
x[ii] = (static_cast<double>(rand()) / RAND_MAX) * 100.0;
y[ii] = (static_cast<double>(rand()) / RAND_MAX) * 100.0;
}
// bench no_ref
auto start = std::chrono::steady_clock::now();
for (uint64_t ii=0; ii < BENCHMARK_RUNS; ii++) {
std::vector<float> ans = vec_mul_no_ref(x, y);
}
auto end = std::chrono::steady_clock::now();
double time = static_cast<double>(
std::chrono::duration_cast<
std::chrono::microseconds>(end-start).count());
std::cout << "Time to multiply vectors (no_ref) = "
<< time / BENCHMARK_RUNS * 1e3 << " ns" << std::endl;
// bench ref_args
start = std::chrono::steady_clock::now();
for (uint64_t ii=0; ii < BENCHMARK_RUNS; ii++) {
std::vector<float> ans = vec_mul_ref_args(x, y);
}
end = std::chrono::steady_clock::now();
time = static_cast<double>(
std::chrono::duration_cast<
std::chrono::microseconds>(end-start).count());
std::cout << "Time to multiply vectors (ref_args) = "
<< time / BENCHMARK_RUNS * 1e3 << " ns" << std::endl;
// bench all_ref
start = std::chrono::steady_clock::now();
for (uint64_t ii=0; ii < BENCHMARK_RUNS; ii++) {
std::vector<float> ans(x.size());
vec_mul_all_ref(x, y, ans);
}
end = std::chrono::steady_clock::now();
time = static_cast<double>(
std::chrono::duration_cast<
std::chrono::microseconds>(end-start).count());
std::cout << "Time to multiply vectors (all_ref) = "
<< time / BENCHMARK_RUNS * 1e3 << " ns" << std::endl;
}
int main() {
bench_vec_mul();
return 0;
}
此代码在我的笔记本电脑上的示例输出(使用 g++ -o benchmark main.cc -std=c++17 -O3
编译)是:
Time to multiply vectors (no_ref) = 5117.05 ns
Time to multiply vectors (ref_args) = 3000.69 ns
Time to multiply vectors (all_ref) = 2996.84 ns
第二次和第三次如此相似表明正在执行返回值优化并且正在省略该拷贝。我的问题是:为什么函数参数的拷贝也没有被编译器删除,以便第一次匹配后两个?将函数参数声明为 const 可确保它们不会更改,因此不会意外编辑原始变量。
我有 gcc (GCC) 8.1.1 20180531
最佳答案
My question is: why isn't the copy for the function arguments also elided by the compiler so that the first time matches the second two?
因为标准不允许。
省略不是偶然发生的事情;它不是无处不在的假设规则的一部分。因为它会影响用户可见的行为(复制/移动构造函数可能是用户定义的代码),所以标准必须明确说明有时,实现可以自由地不调用它们。因此,该标准仅在非常特定的情况下允许省略。
从参数表达式初始化参数时,仅当参数是值类型且参数表达式是该类型的临时值时才允许省略。好吧,如果您想获得技术知识,在 C++17 中,这种情况下没有省略:纯右值将直接初始化参数而不是显示一个临时参数,因此没有复制/移动要省略。
但这里不是这种情况。 x
和 y
不是纯右值,因此不符合省略条件。它们将被复制到这些参数中。
确实,命名对象只有在您返回 时才有资格被省略。即便如此,如果它是该函数的参数,它也不起作用。
关于c++ - 为什么没有为函数参数省略拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550073/
这是我在这里的第一篇文章,也是我第一次使用 C++。我正在查看从 Internet 获得的一些代码,但我对此有疑问。 它有一个 for 循环,像这样: for(cin >> t;t--;) 我明白它在
我目前正在开发一个网站,除其他外,该网站允许用户通过显示或隐藏他们已购买的商品来过滤市场。这适用于基本的 AJAX 调用,该调用传递可用过滤器的当前条件,然后使用 CodeIgniter 的事件记录构
我创建了一个 MWE,其中通过添加 来更改单行解决编译器错误。 以下代码无法编译: import java.util.List; public class MainClass { publi
当我想测试一些 PostgreSQL 函数 FOO() 的行为时,我发现执行类似 SELECT FOO(bar) 的查询很有用,bar一些数据我用作直接输入,而无需从真实表中SELECT。 我读到我们
在 PHP、Java、C++(以及许多其他语言)中,for 循环是这样使用的: for(int i=0;i<10;i++) 如果我已经初始化了i,我该如何省略初始化语句呢? 最佳答案 在 Java、C
我发现我们的 Android 应用出现了一个奇怪的问题,特别是在 4.4 版的 Moto X 上。 在偏好 Activity 中,所有标题的前 8 个字符都在开头用省略号截断。这也发生在溢出菜单和整个
我有一个 XElement,我必须解析它以删除结束标记中的空白。我的代码如下所示: var stringBuilder = new StringBuilder(); using (var string
假设我有两个接口(interface),X和 Y ,它们共享一些字段,但也有独立的字段: interface X { abc: number; foo: number; bar: numb
我有这个模型: var accountSchema = new mongoose.Schema({ 'seeker': { 'fullName': String,
我在 R 中运行一个具有大量时间和位置固定效应的回归。我尝试将一个漂亮的汇总表输出到 Latex 中。我从 stargazer 包切换到 huxtable,因为 stargazer 在忽略固定效果时表
假设我有一个数据框: a df a b c d 1 0 9 10 2 1 10 13 3 NA 11 14 4 3 NA 7 5 4 13 22 现在假设我
我在 R 中运行一个具有大量时间和位置固定效应的回归。我尝试将一个漂亮的汇总表输出到 Latex 中。我从 stargazer 包切换到 huxtable,因为 stargazer 在忽略固定效果时表
我想删除一个属性并返回一个新对象而不改变原始对象。 我知道我们可以像这样使用 Lodash 轻松做到这一点: const profile = { name: 'Maria', age: 30 } _.
我正在通过更改一些内容来修改 javascript 对象。当我重新创建它时,我会得到每个 key 对的索引号。 "0":{...},"1":{...}, 如何删除/省略这些 0、1、2、3、4 数字的
我正在一个非常方便且名称丰富的网站 here 上完成示例之一。 ,具体来说: func applyMutliplication(value: Int, multFunction: Int -> Int
这个问题在这里已经有了答案: Why do java source files require package declarations? (4 个答案) 关闭 6 年前。 我是 Java 的新手,
在下面的代码中: Widget makeWidget() { return Widget(); } void foo(Widget widget) { ... } foo(makeWid
这是我使用下面的调用调用的过程: CALL abc('01-04-2011','14-04-2014','28,29,36,37,38','33,34,35,41,42,43') 但问题是下面提到的查
嘿,我正在寻找一种在 yui 数据表中省略文本的好方法。我的意思是,格式化文本,使其很好地适合其单元格,并且如果文本必须被 chop ,则在其后面有一个椭圆 (...)。 我想在不使用 CSS 选择器
我有一个如下表(记录)。 ID Status AA124 Pass AA125 Pass Z_AA134 Fail Z_AA135
我是一名优秀的程序员,十分优秀!