-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDescribeSingle.cpp
More file actions
102 lines (85 loc) · 2.45 KB
/
Copy pathDescribeSingle.cpp
File metadata and controls
102 lines (85 loc) · 2.45 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Single Threaded Implementation
------------------------------
Calculates the mean, variance, standard deviation, minimum, and maximum
of uniformly distributed random integers
*/
// https://stats.stackexchange.com/questions/25848/how-to-sum-a-standard-deviation
// https://stackoverflow.com/questions/7616511/calculate-mean-and-standard-deviation-from-a-vector-of-samples-in-c-using-boos
//https://stackoverflow.com/a/28574413/18255427
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <numeric>
#include <cmath>
#include <algorithm>
using Clock = std::chrono::high_resolution_clock;
namespace Stats
{
long long count;
double mean;
double var;
double stdev;
int min;
int max;
};
namespace Vars
{
const unsigned int N = std::thread::hardware_concurrency(); //number of threads on device
const unsigned int NumPerThread = 5e6; //number of random numbers to generate per thread
std::vector<int> RandNums(NumPerThread*N);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 1000);
int sz = 0;
}
void AddN()
{
using namespace Vars;
for (unsigned int i=0; i<NumPerThread*N; i++)
{
RandNums[i] = dis(gen);
++sz;
}
}
void Describe()
{
using namespace Vars;
using namespace Stats;
count = RandNums.size();
//https://stackoverflow.com/a/12405793/18255427
mean = 0.0;
std::for_each (RandNums.begin(), RandNums.end(), [&](const double d) {
mean += d/count;
});
var = 0.0;
//Population standard deviation
std::for_each (RandNums.begin(), RandNums.end(), [&](const double d) {
var += ((d - mean) * (d - mean))/count;
});
stdev = sqrt(var);
const auto [min_local, max_local] =
std::minmax_element(begin(RandNums), end(RandNums));
min = *min_local;
max = *max_local;
}
int main()
{
using namespace Vars;
using namespace Stats;
AddN();
auto start_time = Clock::now();
Describe();
std::cout << "\n\nCount = " << count
<< "\nMean = " << mean
<< "\nVariance = " << var
<< "\nStandard Deviation = " << stdev
<< "\nMin = " << min
<< "\nMax = " << max << "\n\n\n";
auto end_time = Clock::now();
std::cout << "Time difference = "
<< std::chrono::duration<double, std::nano>(end_time - start_time).count() << " nanoseconds\n";
std::cout << "size = " << sz << '\n';
}