[英]fewer constant-time iterations take more time - c++ compiler dependence?
I wanted to demonstrate in my class that sampling with pre-ordered probabilities can improve execution time.我想在我的课堂上证明使用预先排序的概率进行采样可以缩短执行时间。 In the code below, the sample()
function is the working horse.在下面的代码中, sample()
函数是工作的马。 The same random variable distribution is stored in two forms: unsorted probabilities (arrays p
and x
), and sorted probabilities (arrays p1
and x1
) - see the main()
function.相同的随机变量分布以两种形式存储:未排序的概率(数组p
和x
)和排序的概率(数组p1
和x1
) - 请参阅main()
函数。 The counter variable counts for loop iterations.计数器变量计数循环迭代。
The results: with (p,x)
input, sample()
takes twice as mush time as with (p1, x1)
but the elapsed execution time is the same or even longer.结果:对于(p,x)
输入, sample()
花费的时间是(p1, x1)
两倍(p1, x1)
但执行时间相同甚至更长。 I tried g++ 7.4.0 compiler at my home laptop Kubuntu 18.04, and I tried different g++ versions at ( wandbox dot org ) with basically same results.我在家用笔记本电脑 Kubuntu 18.04 上尝试了 g++ 7.4.0 编译器,并在( wandbox dot org )尝试了不同的 g++ 版本,结果基本相同。
I don't understand how it's possible: fewer constant-time iterations take same to longer time.我不明白这怎么可能:更少的恒定时间迭代需要更长的时间。
The code:编码:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
inline double runif(){return rand()/double(RAND_MAX);}
double sample(double* p, double* x, int N, double u, unsigned long* count)
{
int k;
for(k=0; (k<N) && (u>p[k]); k++, (*count)++)
u -= p[k];
return x[k];
}
double sample_alias(double* p, double* x, int N, double u)
{
double u1 = u * N;
int K = floor(u1);
double u2 = u1 - K;
return (u2<p[K]) ? *(x+2*K) : *(x+2*K+1);
}
int main()
{
double p[] = {0.2, 0.05, 0.125, 0.5, 0.125};
double x[] = {0, -3, 1, -2, 3};
double p1[] = {0.5, 0.2, 0.125, 0.125, 0.05};
double x1[] = {-2, 0, 3, 1, -3};
double sum;
unsigned long counter;
#define NN 4000000
double *u;
u = (double*)calloc(NN, sizeof(double));
if(u==NULL) perror("Not enough mem!");
srand(5647892);
for (int i=0; i<NN; i++) u[i]=runif();
cout << "Test 1 (unsorted)" << endl;
sum=0.0; counter = 0;
auto begt = std::chrono::steady_clock::now();
for(int i=0; i<NN; i++) sum+=sample(p,x,5,u[i], &counter);
auto endt = std::chrono::steady_clock::now();
auto elapsed = endt - begt;
cout<<sum/double(NN)<<endl<<"Run took "<<elapsed.count()<<", total loop: "<< counter<<endl;
cout << "Test 1 (sorted)" << endl;
sum=0.0; counter = 0;
begt = std::chrono::steady_clock::now();
for(int i=0; i<NN; i++) sum+=sample(p1,x1,5,u[i],&counter);
endt = std::chrono::steady_clock::now();
elapsed = endt - begt;
cout<<sum/double(NN)<<endl<<"Run took "<<elapsed.count()<<", total loop: "<< counter<<endl;
free(u);
return 0;
}
My test output:我的测试输出:
Test 1 (unsorted)
-0.650426
Run took 32114525, total loop: 9205058
Test 1 (sorted)
-0.649237
Run took 40915156, total loop: 4101917
It turns out, it's the trade-off between compiler's capabilities and the complexity of the algorthm.事实证明,这是编译器的能力和算法的复杂性之间的权衡。 The 5-element array was too small for the benefits of ordering to show off and the CPU mechanisms were overtaking this gain. 5 元素阵列太小,无法展示订购的好处,而 CPU 机制正在超越这一收益。 It was only after initializing all arrays ( p
, x
, p1
, and x1
) with more data (circa 30 elements) that the sorted array generates the output times faster than an unsorted array.只有在使用更多数据(大约 30 个元素)初始化所有数组( p
、 x
、 p1
和x1
)之后,排序数组生成的输出时间比未排序数组快。
The proof (a new main()
function):证明(一个新的main()
函数):
int main()
{
// R: p1 <- dhyper( 0:30, 100, 200, 30)
double p[] = {2.365460e-06, 4.149930e-05, 3.463503e-04, 1.831185e-03, 6.890624e-03,
1.965600e-02, 4.420738e-02, 8.049383e-02, 1.209103e-01, 1.519072e-01,
1.612748e-01, 1.458034e-01, 1.128909e-01, 7.516566e-02, 4.315606e-02,
2.139919e-02, 9.167998e-03, 3.391496e-03, 1.081390e-03, 2.963208e-04,
6.947942e-05, 1.385778e-05, 2.332594e-06, 3.278979e-07, 3.795897e-08,
3.550624e-09, 2.612802e-10, 1.454013e-11, 5.743665e-13, 1.433179e-14,
1.695929e-16};
double x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
// R: p1.sort( p1, decreasing=TRUE, index=TRUE)
double p1[] = {1.612748e-01, 1.519072e-01, 1.458034e-01, 1.209103e-01, 1.128909e-01,
8.049383e-02, 7.516566e-02, 4.420738e-02, 4.315606e-02, 2.139919e-02,
1.965600e-02, 9.167998e-03, 6.890624e-03, 3.391496e-03, 1.831185e-03,
1.081390e-03, 3.463503e-04, 2.963208e-04, 6.947942e-05, 4.149930e-05,
1.385778e-05, 2.365460e-06, 2.332594e-06, 3.278979e-07, 3.795897e-08,
3.550624e-09, 2.612802e-10, 1.454013e-11, 5.743665e-13, 1.433179e-14,
1.695929e-16};
double x1[] = {10, 9, 11, 8, 12, 7, 13, 6, 14, 15, 5, 16, 4, 17, 3, 18, 2, 19,
20, 1, 21, 0, 22, 23, 24, 25, 26, 27, 28, 29, 30};
double sum;
unsigned long counter;
#define NN 1000000
double *u;
u = (double*)calloc(NN, sizeof(double));
if(u==NULL) perror("Not enough mem!");
srand(5647892);
for (int i=0; i<NN; i++) u[i]=runif();
int sz = sizeof(p)/sizeof(p[0]);
cout << "Test 1 (unsorted)" << endl;
sum=0.0; counter = 0;
srand(5647892);
auto begt = std::chrono::steady_clock::now();
for(int i=0; i<NN; i++) sum+=sample(p,x,sz,u[i], &counter);
auto endt = std::chrono::steady_clock::now();
auto elapsed = endt - begt;
cout<<sum/double(NN)<<endl<<"Run took "<<elapsed.count()<<", total loop: "<< counter<<endl;
cout << "Test 1 (sorted)" << endl;
sum=0.0; counter = 0;
srand(5647892);
begt = std::chrono::steady_clock::now();
for(int i=0; i<NN; i++) sum+=sample(p1,x1,sz,u[i],&counter);
endt = std::chrono::steady_clock::now();
elapsed = endt - begt;
cout<<sum/double(NN)<<endl<<"Run took "<<elapsed.count()<<", total loop: "<< counter<<endl;
free(u);
return 0;
}
Sample running times:样品运行时间:
Test 1 (unsorted)
10.0038
Run took 23076167, total loop: 10003850
Test 1 (sorted)
10.0047
Run took 14010650, total loop: 3442722
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.