Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -13,10 +13,14 @@
<NoWarn>1701;1702,CS8002</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\BitFaster.Caching.ThroughputAnalysis\FastZipf.cs" Link="FastZipf.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ConsoleTables" Version="2.4.2" />
<PackageReference Include="CsvHelper" Version="28.0.1" />
<PackageReference Include="EasyConsole" Version="1.1.0" >
<PackageReference Include="EasyConsole" Version="1.1.0">
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching.HitRateAnalysis/Zipfian/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;
using BitFaster.Caching.ThroughputAnalysis;
using MathNet.Numerics;
using MathNet.Numerics.Distributions;

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

Expand Down
54 changes: 54 additions & 0 deletions BitFaster.Caching.ThroughputAnalysis/FastZipf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MathNet.Numerics;

namespace BitFaster.Caching.ThroughputAnalysis
{
// produces the same output as MathNet.Numerics Zipf.Samples(random, samples[], s, n)
// but about 20x faster.
public class FastZipf
{
static Random srandom = new Random(666);

public static int[] Generate(Random random, int sampleCount, double s, int n)
{
double[] num = new double[sampleCount];
int[] samples = new int[sampleCount];

for (int i = 0; i < sampleCount; i++)
{
while (num[i] == 0.0)
{
num[i] = random.NextDouble();
}
}

double num2 = 1.0 / SpecialFunctions.GeneralHarmonic(n, s);

Parallel.ForEach(Enumerable.Range(0, samples.Length), (x, j) =>
{
double num3 = 0.0;
int i;

for (i = 1; i <= n; i++)
{
num3 += num2 / Math.Pow(i, s);
if (num3 >= num[x])
{
break;
}
}

samples[x] = i;
});

return samples;
}

public static int[] Generate(int sampleCount, double s, int n)
{
return Generate(srandom, sampleCount, s, n);
}
}
}
4 changes: 4 additions & 0 deletions BitFaster.Caching.ThroughputAnalysis/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using BitFaster.Caching.ThroughputAnalysis;
using Iced.Intel;
using MathNet.Numerics.Distributions;

Host.PrintInfo();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public ZipfConfig(int iterations, int sampleCount, double s, int n)
{
this.iterations = iterations;

Random random = new Random(666);

samples = new int[sampleCount];
Zipf.Samples(random, samples, s, n);
samples = FastZipf.Generate(sampleCount, s, n);
}

public int Iterations => iterations;
Expand Down