-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathpidigits-3.cs
144 lines (132 loc) · 4.21 KB
/
pidigits-3.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Adapted from pidigits C# .NET Core #3 program
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=pidigits&lang=csharpcore&id=3
// aka (as of 2017-09-01) rev 1.2 of https://alioth.debian.org/scm/viewvc.php/benchmarksgame/bench/pidigits/pidigits.csharp-3.csharp?root=benchmarksgame&view=log
// Best-scoring C# .NET Core version as of 2017-09-01
// (also best-scoring single-threaded C# .NET Core version as of 2017-09-01)
// **** Version #3 on website pinvokes to native GMP library; this has been modified to
// use .NET's System.Numerics.BigInteger type instead ****
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
*
* Port of the Java port that uses native GMP to use native GMP with C#
* contributed by Miguel de Icaza, based on the Java version, that was:
* contributed by Mike Pall
* java port by Stefan Krause
*/
using System;
using System.Numerics;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MicroBenchmarks;
namespace BenchmarksGame
{
public class pidigits
{
BigInteger q = new BigInteger(), r = new BigInteger(), s = new BigInteger(), t = new BigInteger();
BigInteger u = new BigInteger(), v = new BigInteger(), w = new BigInteger();
int i;
StringBuilder strBuf = new StringBuilder(40), lastBuf = null;
int n;
pidigits(int n)
{
this.n = n;
}
private void compose_r(int bq, int br, int bs, int bt)
{
u = r * bs;
r *= bq;
v = t * br;
r += v;
t *= bt;
t += u;
s *= bt;
u = q * bs;
s += u;
q *= bq;
}
/* Compose matrix with numbers on the left. */
private void compose_l(int bq, int br, int bs, int bt)
{
r *= bt;
u = q * br;
r += u;
u = t * bs;
t *= bt;
v = s * br;
t += v;
s *= bq;
s += u;
q *= bq;
}
/* Extract one digit. */
private int extract(int j)
{
u = q * j;
u += r;
v = s * j;
v += t;
w = u / v;
return (int)w;
}
/* Print one digit. Returns 1 for the last digit. */
private bool prdigit(int y, bool verbose)
{
strBuf.Append(y);
if (++i % 10 == 0 || i == n)
{
if (i % 10 != 0)
for (int j = 10 - (i % 10); j > 0; j--)
{ strBuf.Append(" "); }
strBuf.Append("\t:");
strBuf.Append(i);
if (verbose) Console.WriteLine(strBuf);
lastBuf = strBuf;
strBuf = new StringBuilder(40);
}
return i == n;
}
/* Generate successive digits of PI. */
void Run(bool verbose)
{
int k = 1;
i = 0;
q = 1;
r = 0;
s = 0;
t = 1;
for (; ; )
{
int y = extract(3);
if (y == extract(4))
{
if (prdigit(y, verbose))
return;
compose_r(10, -10 * y, 0, 1);
}
else
{
compose_l(k, 4 * k + 2, 0, 2 * k + 1);
k++;
}
}
}
public static StringBuilder Bench(int n, bool verbose)
{
pidigits m = new pidigits(n);
m.Run(verbose);
return m.lastBuf;
}
}
[BenchmarkCategory(Categories.Runtime, Categories.BenchmarksGame, Categories.JIT)]
public class PiDigits_3
{
[Benchmark(Description = nameof(PiDigits_3))]
[Arguments(3000, "8649423196\t:3000")]
public StringBuilder RunBench(int n, string expected)
=> pidigits.Bench(n, false);
}
}