简体   繁体   English

提高F# map插入性能

[英]Increase F# map insertion performance

I am currently doing some tests on F# maps vs C# dictionaries.我目前正在对 F# 地图与 C# 字典进行一些测试。 I realize they are quite different implementation wise but they do fill the same sort of use for their respective languages.我意识到它们在实现方面完全不同,但它们确实为各自的语言提供了相同的用途。

I have designed a simple test to check the insertion times due to the F# map being immutable thus it has to create an entirely new map for each insertion.我设计了一个简单的测试来检查插入时间,因为 F# map 是不可变的,因此它必须为每次插入创建一个全新的 map。 I was wondering just how much of a hit that is.我想知道这有多大的打击。

Test is as follows:测试如下:

 //F# 
 module Test = 
    let testMapInsert () = 
        let sw = Stopwatch()
        let rec fillMap endIdx curr map =
            if curr = endIdx then 
                map 
            else 
                fillMap endIdx (curr + 1) (map |> Map.add curr curr)
        sw.Start ()
        let q = fillMap 100000000 Map.empty
        sw.Stop ()
        printfn "%A" sw.ElapsedMilliseconds

 //C#
 class Program
    {
        static void Test(int x) {
            var d = new Dictionary<int,int>();
            for (int i = 0; i < x; i++) {
                d.Add(i,i);
            }
        }
        static void Main(string[] args) {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Test(10000000);
            sw.Stop();
            System.Console.WriteLine(sw.ElapsedMilliseconds);
            //FSHARP.Test.testMapInsert(); f# function called in c#.

        }
    }

Doing 10 million element insertions with this yields the following times measured in ms:使用它进行 1000 万个元素插入会产生以下以 ms 为单位的时间:

C#: 332

F#: 13605

I figured the C# dictionary would be a fair bit faster but that is quite the difference.我认为 C# 字典会快一点,但差别很大。

Is there a way to speed up the F# dictionary for this sort of use-case?有没有办法为这种用例加速 F# 字典? Or is this just the way it is and the F# map has a trade-off with performance in these situations for thread safety?或者这就是它的方式和 F# map 在这些情况下为了线程安全而权衡性能?

As mentioned in the comments, the difference is not based on the distinction between C# and F#, but based on the distinction between an immutable tree-based map and hashtable-based mutable dictionary.正如评论中提到的,区别不是基于 C# 和 F# 之间的区别,而是基于基于不可变树的 map 和基于哈希表的字典之间的区别。

Using #time , I get the following performance in F# interactive:使用#time ,我在 F# 交互式中获得以下性能:

#time 
// Immutable tree-based F# map (~14 sec)
let mutable map = Map.empty
for i in 0 .. 10000000 do
  map <- Map.add i i map

// Mutable hashtable-based .NET dictionary (~0.3 sec)
let dict = System.Collections.Generic.Dictionary<_, _>()
for i in 0 .. 10000000 do
  dict.Add(i, i)

The interesting question is - can you make immutable F# map faster?有趣的问题是——你能让不可变的 F# map 更快吗? In principle, you can build a map faster if you know that you are working with an already sorted array.原则上,如果您知道您正在使用已排序的数组,则可以更快地构建 map。 The F# map does not have any operation that would let you do this, but it could be added. F# map 没有任何操作可以让您执行此操作,但可以添加。

When I define my own Map type that shares the interanl structure with the F# map:当我定义自己的 Map 类型时,它与 F# map 共享内部结构:

type MapTree<'Key, 'Value when 'Key : comparison > = 
  | MapEmpty 
  | MapOne of 'Key * 'Value
  | MapNode of 'Key * 'Value * MapTree<'Key, 'Value> *  MapTree<'Key, 'Value> * int

I can then define ofSortedArray operation:然后我可以定义ofSortedArray操作:

let height = function
  | MapEmpty -> 0
  | MapOne _ -> 1
  | MapNode(_, _, _, _, h) -> h

let rec ofSortedArray (data:_[]) i j = 
  if i = j then MapOne(data.[i])
  elif i > j then MapEmpty 
  else 
    let m = i + (j - i) / 2
    let l, r = ofSortedArray data i (m - 1), ofSortedArray data (m + 1) j
    let k, v = data.[m]
    MapNode(k, v, l, r, 1 + (max (height l) (height r)))

This is still nowhere near as efficient as a mutable hashtable, but I get the following:这仍然远不如可变哈希表有效,但我得到以下信息:

// Immutable tree-based F# map, using sorted array 
let arr = [| for i in 0 .. 10000000 -> i, i |] // ~1 sec
let map = ofSortedArray arr 0 10000000         // ~3 sec

If you actually wanted to use this, you would need your own version of the F# map - or you could send a pull request to the F# core libraries adding support for something like this!如果您真的想使用它,则需要您自己的 F# map 版本 - 或者您可以向 F# 核心库发送拉取请求,添加对此类内容的支持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM