[英]Why is sorting a std::vector of std::tuple's faster than sorting a vector of std::arrays?
I was curious to see whether sorting a vector <vector<int>>
would be slower than sorting a vector <array <int, 3>>
.我很想知道排序
vector <vector<int>>
是否比排序vector <array <int, 3>>
慢。 The dimensions of the vector
is 1000000 by 3, and below is my driver code implementing this: vector
的维度是 1000000 x 3,下面是我的驱动程序代码:
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <vector<int>> v(1000000, vector <int> (3));
srand(time(nullptr));
for(int i = 0; i < 1000000; ++i){
for(int j = 0; j < 3; ++j){
v[i][j] = rand();
}
}
double start = clock();
sort(v.begin(), v.end());
cout << (clock()-start)/(CLOCKS_PER_SEC/1000) << endl;
return 0;
}
Compiling with g++ -O3 sorting_test.cxx
with gcc 7.5.0, I get a runtime of around 300 ms.用
g++ -O3 sorting_test.cxx
和 gcc 7.5.0 编译,我得到大约 300 毫秒的运行时间。 Declaring v
as a vector <array <int, 3>>
halved the runtime to around 149 ms.将
v
声明为vector <array <int, 3>>
将运行时间减半至大约 149 毫秒。
However, declaring v
as a vector <tuple<int, int, int>>
beat out both of the above options, with an average runtime of approximately 100 ms
.但是,将
v
声明为vector <tuple<int, int, int>>
击败了上述两个选项,平均运行时间约为100 ms
。
I can somewhat understand why the array
option is faster than the vector
option ( array
size is a constant expression, unlike the vector
), but I have no idea why the tuple
would beat both of them.我可以理解为什么
array
选项比vector
选项快( array
大小是一个常量表达式,与vector
不同),但我不知道为什么tuple
会击败它们。 Can somebody please explain this to me?有人可以向我解释一下吗?
The code that fills the tuple <int, int, int>
s is填充
tuple <int, int, int>
的代码是
srand(time(nullptr));
for(int i = 0; i < 1000000; ++i){
get <0> (v[i]) = rand();
get <1> (v[i]) = rand();
get <2> (v[i]) = rand();
}
While the disassembly for the whole programs are too large, this demonstrates the core difference between operator<
for array
and tuple
: https://godbolt.org/z/h1Y33e虽然整个程序的反汇编太大了,但这证明了
operator<
for array
和tuple
之间的核心区别: https : //godbolt.org/z/h1Y33e
Essentially, in the tuple version, you have a fixed comparison of 3 elements whereas in the array version, you have a loop.本质上,在元组版本中,您有 3 个元素的固定比较,而在数组版本中,您有一个循环。
Though I'm surprised that the compiler did not unroll the loop.虽然我很惊讶编译器没有展开循环。
Edit: looks like clang does optimize them to both, non-loop code: https://godbolt.org/z/cMExTb (I did not fully read it, but I only see forward jumps)编辑:看起来像 clang 确实将它们优化为两个,非循环代码: https : //godbolt.org/z/cMExTb (我没有完全阅读它,但我只看到向前跳跃)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.