gpt4 book ai didi

c++ - 更少的恒定时间迭代花费更多时间-C++编译器依赖项?

转载 作者:行者123 更新时间:2023-12-01 14:48:38 25 4
gpt4 key购买 nike

我想在类里面证明通过预先排序的概率进行采样可以缩短执行时间。在下面的代码中,sample()函数是最有效的选择。相同的随机变量分布以两种形式存储:未排序的概率(数组px)和排序的概率(数组p1x1)-请参见main()函数。计数器变量计数循环迭代。

结果:输入(p,x)时,sample()的糊涂时间是(p1, x1)的两倍,但是经过的执行时间相同甚至更长。我在家用笔记本电脑Kubuntu 18.04上尝试了g++ 7.4.0编译器,并在(wandbox dot org)尝试了不同的g++版本,结果基本相同。

我不知道这是怎么可能的:更少的恒定时间迭代需要更长的时间。

编码:

#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;
}

我的测试输出:
Test 1 (unsorted)
-0.650426
Run took 32114525, total loop: 9205058
Test 1 (sorted)
-0.649237
Run took 40915156, total loop: 4101917

最佳答案

事实证明,这是编译器功能与算法复杂性之间的权衡。 5个元素的数组太小,无法通过顺序展示来获得好处,而CPU机制正赶超这一优势。只有在使用更多数据(大约30个元素)初始化所有数组(pxp1x1)之后,排序后的数组才能比未排序的数组更快地生成输出。

证明(新的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;
}

示例运行时间:
Test 1 (unsorted)
10.0038
Run took 23076167, total loop: 10003850
Test 1 (sorted)
10.0047
Run took 14010650, total loop: 3442722

关于c++ - 更少的恒定时间迭代花费更多时间-C++编译器依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com