【发布时间】:2016-04-26 16:56:34
【问题描述】:
我使用循环展开 (LU) 实现了三个版本的冒泡排序来研究 c++ 模板。即,没有循环展开,使用 C 宏手动展开循环,以及使用模板展开循环。代码:
// No LU
void bubbleSort(int* data, int n)
{
for(int i=n-1; i>0; --i) {
for(int j=0; j<i; ++j)
if (data[j]>data[j+1]) std::swap(data[j], data[j+1]);
}
}
// LU with macro
void bubbleSort4(int* data)
{
#define COMP_SWAP(i, j) if(data[i]>data[j]) std::swap(data[i], data[j])
COMP_SWAP(0, 1); COMP_SWAP(1, 2); COMP_SWAP(2, 3);
COMP_SWAP(0, 1); COMP_SWAP(1, 2);
COMP_SWAP(0, 1);
}
// LU with template
template<int i, int j>
inline void IntSwap(int* data) {
if(data[i]>data[j]) std::swap(data[i], data[j]);
}
template<int i, int j>
inline void IntBubbleSortLoop(int* data) {
IntSwap<j, j+1>(data);
IntBubbleSortLoop<j<i-1?i:0, j<i-1?(j+1):0>(data);
}
template<>
inline void IntBubbleSortLoop<0, 0>(int*) { }
template<int n>
inline void IntBubbleSort(int* data) {
IntBubbleSortLoop<n-1, 0>(data);
IntBubbleSort<n-1>(data);
}
template<>
inline void IntBubbleSort<1>(int* data) { }
测试代码:
#include <iostream>
#include <utility> //std::swap
#include <string.h> // memcpy
#include <sys/time.h>
class Timer{
struct timeval start_t;
struct timeval end_t;
public:
void start() { gettimeofday(&start_t, NULL); }
void end() { gettimeofday(&end_t, NULL); }
void print() {
std::cout << "Timer: " << 1000.0*(end_t.tv_sec-start_t.tv_sec)+(end_t.tv_usec-start_t.tv_usec)/1000.0 << " ms\n";
}
};
int main() {
Timer t1, t2, t3; const int num=100000000;
int data[4]; int inidata[4]={3,4,2,1};
t1.start();
for(int i=0; i<num; ++i) {
memcpy(data, inidata, 4);
bubbleSort(data, 4);
}
t1.end();
t1.print();
t2.start();
for(int i=0; i<num; ++i) {
memcpy(data, inidata, 4);
bubbleSort4(data);
}
t2.end();
t2.print();
t3.start();
for(int i=0; i<num; ++i) {
memcpy(data, inidata, 4);
IntBubbleSort<4>(data);
}
t3.end();
t3.print();
return 0;
}
我的平台是OSX,编译器是clang:
g++ -std=c++11 -o loop_unrolling loop_unrolling.cpp
我希望模板版本与宏版本具有相似的性能,比普通版本快得多。但是,模板版本是最慢的。有人知道为什么吗?
Timer: 1847.78 ms
Timer: 685.736 ms
Timer: 5075.86 ms
==================================
使用 -O1:
Timer: 861.071 ms
Timer: 495.001 ms
Timer: 2793.02 ms
与-O2:
Timer: 247.691 ms
Timer: 258.666 ms
Timer: 254.466 ms
使用 -O3:
Timer: 242.535 ms
Timer: 233.354 ms
Timer: 251.297 ms
让我困惑的是,无论我是否启用 -Ox,模板版本都应该生成循环展开代码。但它看起来并不真实。更让我困惑的是,在不启用-Ox的情况下,它甚至比非LP版本还要慢。
【问题讨论】:
-
实际编译时使用的优化级别是?
-
我认为这些“为什么我的代码很慢”的问题也没有正在使用的优化级别值得一票否决。
-
@PaulMcKenzie 你可能是对的。该要求属于minimal reproducible example,不是吗?
-
所以你问...为什么 C++ 是这样的?
-
使用Demo,对于相同的代码,我得到了 100 毫秒的差异,因此这些测量值并不可靠。
标签: c++ templates metaprogramming template-meta-programming