Wow, well, yea, it looks as if struct fields in .NET aren't so zippy, or something. I removed the node parameter from expand, and instead passed in x_key and x_idx.

let expand (xs:node []) (ix:int) (x_key:int) (x_idx:int): int =
if x_idx < 0 then ix
else
xs.[ix+1] <- (new node (x_key &&& (~~~ (1 <<< x_idx)), x_idx-1))
xs.[ix+2] <- (new node (x_key, x_idx-1))
ix+2

let rec trav (acc:int) (xs:node []) (ix:int) : int = if ix < 0 then acc else trav (acc+1) xs (expand xs (ix-1) (xs.[ix].key) (xs.[ix].idx))

Result? In FSI, this is now as fast as the int array version.

Someone who knows a lot more about the JIT/optimizer can probably shed some light...

By on 3/24/2009 9:14 PM ()

Hi MichaelGG.

Thank you! It works great.

Current .NET implementations (both Microsoft.NET 3.5SP1 and Mono 2.x) seem to have issues with passing structs.

I tried pretty much the same with C# and got very similar results (both with Microsoft.NET and Mono/Linux).
In fact F# even outperforms its C# counterpart...

Best Regards,
Stefan.

By on 3/25/2009 6:56 AM ()

I found an even better way, which performs significantly faster than the array method (1.4s versus 2.6s). Inline the expand method:

let inline expand (xs:node []) (ix:int) (x:node) : int =

Hopefully some day the compiler or JIT will handle all this for us :).

By on 3/25/2009 7:58 PM ()

And one more way, not as good as inlining: don't pass in x at all. Do a local "let x = xs.[ix+1]" inside expand, instead. That drops time from 4.3s to 2.6s for me.

By on 3/25/2009 8:07 PM ()

Lots of options:) Good...

Finally I will need to find a good compromise between "raw" performance of the expand/traverse-loop and proper, clean function interfaces.

In that code snippet, "traverse" takes no time and "expand" very little time. In real-world search, "expand" actually incurs some more costs...

Thank you again!

Best Regards,
Stefan

By on 3/27/2009 2:48 AM ()

Did you try with the latest .NET? Struct performance has been not fabulous -- but .NET 3.5SP1 had several improvements for them. I've even had classes outperform structs sometimes! How fast is that for you?

Without looking, this is probably a runtime issue, not F#.

By on 3/24/2009 5:33 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper