Skip to content

Commit fa40263

Browse files
committed
Slight optimizations (about 10-20% faster) to cpp-gmp.cpp
1 parent 46ab920 commit fa40263

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

cpp-gmp.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ void calcPi(unsigned short threadid) {
2424
mpf_t value;
2525
mpf_t divisor;
2626
mpf_t one;
27+
mpf_t two;
2728
mpf_t nptr;
2829

2930
mpf_init(local_pi);
3031
mpf_init(value);
3132
mpf_init(divisor);
3233
mpf_init2(one, 53);
3334
mpf_set_d(one, 1.0);
35+
mpf_init2(two, 53);
36+
mpf_set_d(two, 2.0);
3437
mpf_init2(nptr, 53);
3538

3639
for (unsigned long long n=start; n<=end; n++) {
37-
mpf_set_d(value, 1.0);
38-
mpf_set_d(divisor, 2.0);
40+
//mpf_set_d(value, 1.0);
41+
//mpf_set_d(divisor, 2.0);
3942
mpf_set_d(nptr, n);
4043

41-
mpf_mul(divisor, divisor, nptr); // divisor = 2 * n
44+
mpf_mul(divisor, two, nptr); // divisor = 2 * n
4245
mpf_add(divisor, divisor, one); // divisor = 2 * n + 1
43-
mpf_div(value, value, divisor); // value = 1 / (2 * n + 1)
46+
mpf_div(value, one, divisor); // value = 1 / (2 * n + 1)
4447

4548
if (lastAddition)
4649
mpf_sub(local_pi, local_pi, value);
@@ -57,13 +60,11 @@ void calcPi(unsigned short threadid) {
5760
void run(unsigned short threadsCount, unsigned long long totalRounds) {
5861
std::vector<std::thread> threads(threadsCount);
5962
if ((totalRounds == 0 || threadsCount == 0)) {
60-
printf("Either totalRounds or timeout must be specified or else the program runs forever\n");
63+
printf("TotalRounds and threadsCount must not be 0\n");
6164
exit(1);
6265
}
63-
if (totalRounds) {
64-
splitRounds = checkTotalRounds = totalRounds;
65-
checkTotalRounds *= threadsCount;
66-
}
66+
splitRounds = checkTotalRounds = totalRounds;
67+
checkTotalRounds *= threadsCount;
6768
for (unsigned short threadid=0; threadid<threadsCount; threadid++) {
6869
threads[threadid] = std::thread(calcPi, threadid);
6970
}

0 commit comments

Comments
 (0)