Skip to content

Commit

Permalink
- added benchmark tests for List<T> vs ImmutableList<T> vs FSharpList<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
theburningmonk committed Jan 31, 2014
1 parent 6e0a16e commit 8179eaa
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 40 deletions.
15 changes: 13 additions & 2 deletions RunBenchmarks.fsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#I "examples/BinarySerializersBenchmark/bin/Release"
#I "examples/JsonSerializersBenchmark/bin/Release"
#I "examples/CollectionBenchmark/bin/Release"
#load "packages/FSharp.Charting.0.90.5/FSharp.Charting.fsx"

#r "SimpleSpeedTester.dll"
#r "BinarySerializersBenchmark.dll"
#r "JsonSerializersBenchmark.dll"
#r "CollectionBenchmark.dll"

open System
open System.Collections.Generic
Expand Down Expand Up @@ -75,13 +77,22 @@ let runJsonBenchmark () =
printfn "all done."
prettyPrint results

let runListBenchmark () =
printfn "------- Lists --------"
printfn "Running Benchmarks...\n\n\n\n\n"

let results = CollectionSpeedTest.Run()

printfn "all done."

let rec choose () =
printfn "Choose benchmark to run:\n 1. Binary serializers\n 2. JSON serializers\n 3. Exit"
printfn "Choose benchmark to run:\n 1. Binary serializers\n 2. JSON serializers\n 3. Lists\n 4. Exit"
let answer = Console.ReadLine()
match answer with
| "1" -> runBinaryBenchmark()
| "2" -> runJsonBenchmark()
| "3" -> printfn "bye!"
| "3" -> runListBenchmark()
| "4" -> printfn "bye!"
| _ -> printfn "Sorry, I don't recognize that answer, please enter 1, 2, or 3"
choose()

Expand Down
13 changes: 13 additions & 0 deletions SimpleSpeedTester.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinarySerializersBenchmark"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonSerializersBenchmark", "examples\JsonSerializersBenchmark\JsonSerializersBenchmark.csproj", "{65E36753-C531-417C-AA59-B99D10E68EE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CollectionBenchmark", "examples\CollectionBenchmark\CollectionBenchmark.csproj", "{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -105,6 +107,16 @@ Global
{65E36753-C531-417C-AA59-B99D10E68EE3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{65E36753-C531-417C-AA59-B99D10E68EE3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{65E36753-C531-417C-AA59-B99D10E68EE3}.Release|x86.ActiveCfg = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Debug|x86.ActiveCfg = Debug|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Any CPU.Build.0 = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -114,6 +126,7 @@ Global
{7ADA7026-5757-4E93-AC72-D7E69DC08433} = {F891B8A3-232F-4736-9CF7-1C1D2BDCB6F8}
{564AA4F6-333F-41D2-B924-A07B3D449BDC} = {F891B8A3-232F-4736-9CF7-1C1D2BDCB6F8}
{65E36753-C531-417C-AA59-B99D10E68EE3} = {F891B8A3-232F-4736-9CF7-1C1D2BDCB6F8}
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10} = {F891B8A3-232F-4736-9CF7-1C1D2BDCB6F8}
{207E91B1-C9F8-4913-88E1-3549EF5F3273} = {8FCC7912-0E7B-4520-9BAA-30443A57DD61}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BinarySerializersSpeedTest.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
18 changes: 0 additions & 18 deletions examples/BinarySerializersBenchmark/Program.cs

This file was deleted.

6 changes: 6 additions & 0 deletions examples/CollectionBenchmark/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
75 changes: 75 additions & 0 deletions examples/CollectionBenchmark/CollectionBenchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CollectionBenchmark</RootNamespace>
<AssemblyName>CollectionBenchmark</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Collections.Immutable">
<HintPath>..\..\packages\Microsoft.Bcl.Immutable.1.0.30\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CollectionSpeedTest.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SimpleSpeedTester\SimpleSpeedTester.csproj">
<Project>{87b76a3e-3932-47f1-924d-c80a1ae92787}</Project>
<Name>SimpleSpeedTester</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
112 changes: 112 additions & 0 deletions examples/CollectionBenchmark/CollectionSpeedTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

using Microsoft.FSharp.Collections;

using SimpleSpeedTester.Core;
using SimpleSpeedTester.Interfaces;

namespace SimlpeSpeedTester.Example
{
public class CollectionSpeedTest
{
// test serialization and deserialization on 100k objects
private const int ObjectsCount = 100000;

// perform 5 runs of each test
private const int TestRuns = 1;

public static Dictionary<string, Tuple<ITestResultSummary, ITestResultSummary>> Run()
{
var results = new Dictionary<string, Tuple<ITestResultSummary, ITestResultSummary>>();

results.Add("List<T> with value type", AddItemsWithListT("List<T> with value type", 42));
results.Add("List<T> with ref type", AddItemsWithListT("List<T> with ref type", "42"));

results.Add("ImmutableList<T> with value type", AddItemsWithImmutableListT("ImmutableList<T> with value type", 42));
results.Add("ImmutableList<T> with ref type", AddItemsWithImmutableListT("ImmutableList<T> with ref type", "42"));

results.Add("FSharpList<T> with value type", AddItemsWithFSharpListT("FSharpList<T> with value type", 42));
results.Add("FSharpList<T> with ref type", AddItemsWithFSharpListT("FSharpList<T> with ref type", "42"));

return results;
}

private static Tuple<ITestResultSummary, ITestResultSummary> AddItemsWithListT<T>(string testGroupName, T item)
{
var list = new List<T>();

var testGroup = new TestGroup(testGroupName);

var addResultSummary =
testGroup
.Plan("Add", () => Enumerable.Range(0, ObjectsCount).ForEach(_ => list.Add(item)), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(addResultSummary);

var removeResultSummary =
testGroup
.Plan("Remove From Head", () => Enumerable.Range(0, ObjectsCount).ForEach(_ => list.RemoveAt(0)), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(removeResultSummary);

return Tuple.Create(addResultSummary, removeResultSummary);
}

private static Tuple<ITestResultSummary, ITestResultSummary> AddItemsWithImmutableListT<T>(string testGroupName, T item)
{
var list = ImmutableList<T>.Empty;

var testGroup = new TestGroup(testGroupName);

var addResultSummary =
testGroup
.Plan("Add", () => Enumerable.Range(0, ObjectsCount).ForEach(_ => list = list.Add(item)), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(addResultSummary);

var removeResultSummary =
testGroup
.Plan("Remove From Head", () => Enumerable.Range(0, ObjectsCount).ForEach(_ => list = list.RemoveAt(0)), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(removeResultSummary);

return Tuple.Create(addResultSummary, removeResultSummary);
}

private static Tuple<ITestResultSummary, ITestResultSummary> AddItemsWithFSharpListT<T>(string testGroupName, T item)
{
var list = FSharpList<T>.Empty;

var testGroup = new TestGroup(testGroupName);

var addResultSummary =
testGroup
.Plan("Add", () => Enumerable.Range(0, ObjectsCount).ForEach(_ => list = FSharpList<T>.Cons(item, list)), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(addResultSummary);

var removeResultSummary =
testGroup
.Plan("Remove From Head", () => Enumerable.Range(0, ObjectsCount).ForEach(i => list = list.Tail), TestRuns)
.GetResult()
.GetSummary();

Console.WriteLine(removeResultSummary);

return Tuple.Create(addResultSummary, removeResultSummary);
}
}
}
16 changes: 16 additions & 0 deletions examples/CollectionBenchmark/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

namespace SimlpeSpeedTester.Example
{
public static class EnumerableExtensions
{
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var i in source)
{
action(i);
}
}
}
}
36 changes: 36 additions & 0 deletions examples/CollectionBenchmark/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CollectionBenchmark")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CollectionBenchmark")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3f3e2c92-83d9-493c-8035-2919df0d061c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions examples/CollectionBenchmark/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.Immutable" version="1.0.30" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="JsonSerializersSpeedTest.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleJson.cs" />
</ItemGroup>
Expand Down
18 changes: 0 additions & 18 deletions examples/JsonSerializersBenchmark/Program.cs

This file was deleted.

1 change: 1 addition & 0 deletions packages/repositories.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\examples\BinarySerializersBenchmark\packages.config" />
<repository path="..\examples\CollectionBenchmark\packages.config" />
<repository path="..\examples\JsonSerializersBenchmark\packages.config" />
<repository path="..\tests\SimpleSpeedTester.Tests\packages.config" />
</repositories>

0 comments on commit 8179eaa

Please sign in to comment.