Skip to content

Commit 45b96d9

Browse files
authored
faster zipf (#280)
* zipf * rem props * config
1 parent a59a91b commit 45b96d9

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

BitFaster.Caching.HitRateAnalysis/BitFaster.Caching.HitRateAnalysis.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -13,10 +13,14 @@
1313
<NoWarn>1701;1702,CS8002</NoWarn>
1414
</PropertyGroup>
1515

16+
<ItemGroup>
17+
<Compile Include="..\BitFaster.Caching.ThroughputAnalysis\FastZipf.cs" Link="FastZipf.cs" />
18+
</ItemGroup>
19+
1620
<ItemGroup>
1721
<PackageReference Include="ConsoleTables" Version="2.4.2" />
1822
<PackageReference Include="CsvHelper" Version="28.0.1" />
19-
<PackageReference Include="EasyConsole" Version="1.1.0" >
23+
<PackageReference Include="EasyConsole" Version="1.1.0">
2024
<NoWarn>NU1701</NoWarn>
2125
</PackageReference>
2226
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />

BitFaster.Caching.HitRateAnalysis/Zipfian/Runner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using BitFaster.Caching.Lru;
8+
using BitFaster.Caching.ThroughputAnalysis;
89
using MathNet.Numerics;
910
using MathNet.Numerics.Distributions;
1011

@@ -58,8 +59,7 @@ public static void Run()
5859
{
5960
Console.WriteLine($"Generating Zipfian distribution with {sampleCount} samples, s = {sValues[index]}, N = {n}");
6061
var sw = Stopwatch.StartNew();
61-
zipdfDistribution[index] = new int[sampleCount];
62-
Zipf.Samples(zipdfDistribution[index], sValues[index], n);
62+
zipdfDistribution[index] = FastZipf.Generate(new Random(666), sampleCount, sValues[index], n);
6363
Console.WriteLine($"Took {sw.Elapsed} for s = {sValues[index]}.");
6464
});
6565

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using MathNet.Numerics;
5+
6+
namespace BitFaster.Caching.ThroughputAnalysis
7+
{
8+
// produces the same output as MathNet.Numerics Zipf.Samples(random, samples[], s, n)
9+
// but about 20x faster.
10+
public class FastZipf
11+
{
12+
static Random srandom = new Random(666);
13+
14+
public static int[] Generate(Random random, int sampleCount, double s, int n)
15+
{
16+
double[] num = new double[sampleCount];
17+
int[] samples = new int[sampleCount];
18+
19+
for (int i = 0; i < sampleCount; i++)
20+
{
21+
while (num[i] == 0.0)
22+
{
23+
num[i] = random.NextDouble();
24+
}
25+
}
26+
27+
double num2 = 1.0 / SpecialFunctions.GeneralHarmonic(n, s);
28+
29+
Parallel.ForEach(Enumerable.Range(0, samples.Length), (x, j) =>
30+
{
31+
double num3 = 0.0;
32+
int i;
33+
34+
for (i = 1; i <= n; i++)
35+
{
36+
num3 += num2 / Math.Pow(i, s);
37+
if (num3 >= num[x])
38+
{
39+
break;
40+
}
41+
}
42+
43+
samples[x] = i;
44+
});
45+
46+
return samples;
47+
}
48+
49+
public static int[] Generate(int sampleCount, double s, int n)
50+
{
51+
return Generate(srandom, sampleCount, s, n);
52+
}
53+
}
54+
}

BitFaster.Caching.ThroughputAnalysis/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Diagnostics;
24
using BitFaster.Caching.ThroughputAnalysis;
5+
using Iced.Intel;
6+
using MathNet.Numerics.Distributions;
37

48
Host.PrintInfo();
59

BitFaster.Caching.ThroughputAnalysis/ThroughputBenchConfig.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public ZipfConfig(int iterations, int sampleCount, double s, int n)
2525
{
2626
this.iterations = iterations;
2727

28-
Random random = new Random(666);
29-
30-
samples = new int[sampleCount];
31-
Zipf.Samples(random, samples, s, n);
28+
samples = FastZipf.Generate(sampleCount, s, n);
3229
}
3330

3431
public int Iterations => iterations;

0 commit comments

Comments
 (0)