auto startTime = high_resolution_clock::now();
vector<int> v(10000, 5);
for(auto& a : v)
a *= a;
auto stopTime = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stopTime - startTime);
cout<<"time: "<<duration.count()*1000<<" microsec\n"; //98362000 microsec
from datetime import datetime
start = datetime.now()
a = [5]*10000
a = [a ** 2 for a in a]
end = datetime.now()
diff = end - start
print("time: ", diff.microseconds , " microseconds") //1542 microseconds
从上面看,Python是明显的赢家,但C++的下级(我觉得)为什么它的慢WRT Python?p>
我知道chrono和datetime软件包的时间计算算法可能不同,但向量(c++)和列表(python)的比较基准是什么
Demo on Compiler Explorer
注意:确保您正在测试优化的构建以获得有用的结果
相关问题 更多 >
编程相关推荐