C++编程语言第4版,第225页读取:只要结果与简单执行顺序的结果相同,编译器就可以重新排序代码以提高性能.一些编译器,例如发布模式下的Visual C++,将重新排序此代码:
#include <time.h>
...
auto t0 = clock();
auto r = veryLongComputation();
auto t1 = clock();
std::cout << r << " time: " << t1-t0 << endl;
Run Code Online (Sandbox Code Playgroud)
进入这种形式:
auto t0 = clock();
auto t1 = clock();
auto r = veryLongComputation();
std::cout << r << " time: " << t1-t0 << endl;
Run Code Online (Sandbox Code Playgroud)
这保证了与原始代码不同的结果(零与大于零的时间报告).有关详细示例,请参阅我的其他问题 这种行为是否符合C++标准?
在这段代码中,我正在比较两个功能相同的循环的性能:
for (int i = 1; i < v.size()-1; ++i) {
int a = v[i-1];
int b = v[i];
int c = v[i+1];
if (a < b && b < c)
++n;
}
Run Code Online (Sandbox Code Playgroud)
和
for (int i = 1; i < v.size()-1; ++i)
if (v[i-1] < v[i] && v[i] < v[i+1])
++n;
Run Code Online (Sandbox Code Playgroud)
在优化标志设置为O2
:的许多不同C++编译器中,第一个运行速度明显慢于第二个编译器:
我很困惑,现代C++优化器在处理这种情况时遇到了问题.任何线索为什么?我是否必须编写丑陋的代码而不使用临时变量才能获得最佳性能?
现在,使用临时变量可以使代码更快,有时甚至更快.到底是怎么回事?
我正在使用的完整代码如下:
#include <algorithm>
#include <chrono>
#include <random>
#include <iomanip>
#include <iostream>
#include …
Run Code Online (Sandbox Code Playgroud) 对这个课程进行基准测试:
struct Sieve {
std::vector<bool> isPrime;
Sieve (int n = 1) {
isPrime.assign (n+1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= (int)sqrt((double)n); ++i)
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = false;
}
};
Run Code Online (Sandbox Code Playgroud)
在调用大量构造函数时,我的性能(CPU时间)比64位二进制版本与32位版本(发布版本)差3倍,例如
Sieve s(100000000);
Run Code Online (Sandbox Code Playgroud)
我测试了sizeof(bool)
它,它适用1
于两个版本.当我vector<bool>
用vector<char>
64位和32位版本替换性能时,它会变得相同.这是为什么?
以下是S(100000000)
(释放模式,32位优先,64位秒)的运行时间):
vector<bool>
0.97s 3.12s
vector<char>
0.99s 0.99s
vector<int>
1.57s 1.59s
我还对VS2010进行了一次完整性测试(由Wouter Huysentruit的回应提示),产生0.98秒0.88秒.所以VS2012实施有问题.
我向Microsoft Connect提交了一个错误报告
编辑
下面的许多答案都评论了使用int …
我在 Intel i3-N305 3.8GHz 和 AMD Ryzen 7 3800X 3.9GHz PC 上运行了使用 gcc-13 ( https://godbolt.org/z/qq5WrE8qx )编译的相同二进制文件。此代码使用 VCL 库(https://github.com/vectorclass/version2):
int loop_vc_nested(const array<uint8_t, H*W> &img, const array<Vec32uc, 8> &idx) {
int sum = 0;
Vec32uc vMax, iMax, vCurr, iCurr;
for (int i=0; i<H*W; i+=W) {
iMax.load(&idx[0]);
vMax.load(&img[i]);
for (int j=1; j<8; j++) {
iCurr.load(&idx[j]);
vCurr.load(&img[i+j*32]);
iMax = select(vCurr > vMax, iCurr, iMax);
vMax = max(vMax, vCurr);
}
Vec32uc vMaxAll{horizontal_max(vMax)};
sum += iMax[horizontal_find_first(vMax == vMaxAll)];
}
return sum;
}
Run Code Online (Sandbox Code Playgroud)
完整的基准源位于:https://github.com/pauljurczak/simd-benchmarks/blob/main/main-5-vcl-eve.cpp …
我正在使用Visual Studio 2015 Update 1 C++编译器和此代码段:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{3, 1, 4};
v.reserve(6);
for (auto e: v)
v.push_back(e*e);
for (auto e: v)
cout << e << " ";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
发布版本运行正常,但调试版本会生成vector iterators incompatible
错误消息.这是为什么?
在将基于范围的循环c ++ 11中的元素添加到向量之前将其标记为重复问题之前,请阅读我的答案 /sf/answers/2482748201/,其中包含相反的参数.
是否有更短的符号功能(有趣的x - > x),然后定义自己的?写作
Seq.countBy (fun x -> x)
Run Code Online (Sandbox Code Playgroud)
看起来很罗嗦.
这段代码片段:
enum {N = 10, M = 100};
vector<int> v(N, M);
Run Code Online (Sandbox Code Playgroud)
由于以下错误,无法使用Visual Studio 2013进行编译:
错误C2838:'iterator_category':成员声明中的非法限定名称
它出什么问题了?
在获取Array,List或Seq的第N个元素的函数中有不同的参数顺序是否有充分的理由:
Array.get source index
List .nth source index
Seq .nth index source
Run Code Online (Sandbox Code Playgroud)
我想使用管道运算符,它似乎只能用Seq:
s |> Seq.nth n
Run Code Online (Sandbox Code Playgroud)
有没有办法与Array或List使用相同的表示法?
Visual C++ 2017和gcc 5.4 在此代码段中产生但不是conversion from 'const unsigned char' to 'const float' requires a narrowing conversion
警告:Line B
Line A
#include <iostream>
int main() {
const unsigned char p = 13;
const float q = p; // Line A
std::cout << q << '\n';
const unsigned char c[3] = {0, 1, 255};
const float f[3] = {c[2], c[0], c[1]}; // Line B
for (auto x:f)
std::cout << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
这个警告有效吗?为什么Line B
治疗不同于Line A
?
c++ ×7
c++11 ×2
f# ×2
optimization ×2
performance ×2
stl ×2
syntax ×2
avx2 ×1
benchmarking ×1
boolean ×1
clock ×1
gcc ×1
gcc-warning ×1
kotlin ×1
pipeline ×1
simd ×1
vector ×1
visual-c++ ×1