Rewriting from C# into F#.

June 29, 2013

I know this is almost an old story by now, but it’s the first time I’ve been able to produce any numbers myself. :)

Recently, I rewrote into F# a super simple data transferring app that I’d built for work last fall in C#. There’s not much to it: I grab data from a couple different databases, smash it together in multiple and occasionally bizarre ways, and send it on to its new database home. It’s a perfect use case for F#.

Struggles

I ran into a couple issues along the way with the SQLEntityConnection and SQLDataConnection type providers.

First, a “mapping of CLR type to EDM type is ambiguous” error. I was using the SQLEntityConnection type provider for all the connections, but Entity Framework doesn’t allow mapping from two databases with any of the same table names.

So, I switched to using the SQLDataConnection type provider. For one database, it worked like a charm. (Progress!) A second one, not so much. I was confronted with somewhere near 680 schema errors. Eeek. After much (MUCH) random flailing about, I discovered that setting StoredProcedures = false for the connections rights everything. I worked out the rest of the code in a few hours this morning. Awesome!

Findings

First: during the rewrite, I noticed that I was actually processing tremendously more data than I needed. Heh, oops? This wasn’t as obvious in the C# code, and I’d apparently glossed over it. Going back and fixing this in the C# version actually brought the total processing time down to about 7 minutes (7:14.119 to be exact) from a couple hours.

70% less time!
Second, the longest part of the app, somewhat obviously and by far, was the middle step of processing the data. For the F# version, I used an Array.Parallel.partition to split out and then process what I needed. That, and a couple other details brought the total app time down to nearly 2 minutes (2:24.145.)

25% less code!
Third and finally, I ran a line count in C# (VS shows 140 lines of code, I’m guessing that doesn’t count brackets? Because that seems way too low for 2 projects and 15 total files.) The F# version is 103.

F# is the best ever.

Times, btw, are DateTime.Now differences from the first line of main, until just before the final line is printed to the console. Nothing fancy.

posted in F#, type providers by rach

Follow comments via the RSS Feed | Leave a comment | Trackback URL

5 Comments to "Rewriting from C# into F#."

  1. Peter Ritchie wrote:

    Cool. I expected the code to be smaller, what about the IL? Does it generate less, more, same, IL? (not that that really matters :)

  2. rach wrote:

    The F# code to be smaller? VS doesn’t calculate F# metrics on code, so I counted up those lines by looking at the final line numbers. So spaces and opens were all included, which I bet aren’t in the C#.

    Ooo, interesting. I didn’t even think about the IL. I’ll have to check, because now I wonder. :)

  3. David Grenier wrote:

    Hey Rachel,

    Thanks for the tip about StoredProcedure=false, I didn’t know about this and is what was preventing me from using the autogenerated schema. Been using my own dbml, will see whether that gets the job done.

    It’s true that fsharp is the best :D

  4. rach wrote:

    Oh cool David! Glad I could help.

    Let me know if that gets you back to using the automated schema! I’d be super interested. :)

  5. AnneTheAgile wrote:

    hi, you all may know this, but there is a VS2013 [1] [and prior, starting VS2010], [2] command line tool by MS based on FxCop that will tell code [lines] or IL size , the same as in the GUI Code metrics. This can be hooked up for CI use.
    [1] VS2013 ; Command line utility to calculate code metrics for your .NET code ; Published 2014.0117; X.Download Visual Studio Code Metrics Powertool for Visual Studio 2013 from Official Microsoft Download Center
    http://www.microsoft.com/en-us/download/details.aspx?id=41647
    [2] Code metrics from the command line – Skinner’s Blog – Site Home – MSDN Blogs
    http://blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org