Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Console.WriteLine() vs printfn in F#

Tags:

What is the difference between the following two statements in F#? Are they any advantages or disadvantages compared to each other (excluding the obvious syntax differences)?

I understand that WriteLine() is part of .NET, but do not understand what implications this might have.

The Sample Code:

printfn "This is an integer: %d" 5 System.Console.WriteLine("This is an integer: {0}" , 5) 
like image 574
OMGtechy Avatar asked Jun 12 '13 17:06

OMGtechy


People also ask

What is Console WriteLine() good for?

Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.

What is Printfn?

The Print. F# has two basic functions; “printf” and “printfn” to print out the information on console. // Theprintf/printfn functions are similar to the. // Console. Write/WriteLine functions in C#.

How to Write Console WriteLine in c#?

Console. WriteLine("This is C#"); In this code line, we print the "This is C#" string to the console. To print a message to the console, we use the WriteLine method of the Console class.


2 Answers

printfn and its various cousins have several advantages:

  • They're shorter.
  • They can do some static type checking; i.e. printfn "%d" "bad type" will not compile.
  • ...but you don't have to do static type checking; %O prints any object
  • They can print "smart" representations for things like arrays, tuples, and discriminated unions with %A
  • They can be partially applied; i.e. printfn "%d, %d" 3 is a valid expression. This is particularly nifty since the compiler can check that you actually apply the right number of arguments when you later use this subexpression - unlike Console.WriteLine which will happily accept too many or too few parameters.

In practice, the most common partial application is likely to include just the format string; e.g.

let printParticle = printfn "Particle at (%d, %d), state %A, p = %f"  printParticle 2 3 //compile time warning about ignored value printParticle 3 4 someState 0.4 //fine printParticle 5 6 someState 0.4 0.7 //compile-time error 

However, prior to F# 3.1, it's also slow. It's plenty fast enough to keep up with you the coder, but if you're using it in some form of serialization, it could turn into a bottleneck. The F# 3.1 release announcement (which is distributed as part of Visual Studio 2013) claims to improve the performance dramatically, though I have not verified this.

Personally, I usually use printfn for exploratory coding, and then I largely stick to %A with the occasional other specifier thrown in. However, the .NET native string formatting is still useful in some cases for its detailed culture and formatting-related options. If you want maximum speed direct concatenation (or a StringBuilder) will easily outperform both as this avoids interpreting the format string.

like image 73
Eamon Nerbonne Avatar answered Sep 21 '22 18:09

Eamon Nerbonne


Here are some pros and cons of printf-like functions compared to Console.WriteLine.

Pros:

  • printfn functions are type-safe:

    printfn "This is an integer: %i" 5 // works printfn "This is an integer: %i" "5" // doesn't compile 
  • It's easy to do partial application with printfn, which is not the case with Console.WriteLine due to excessive number of overloads:

    [1; 2; 3] |> List.iter (printfn "%i; ") 
  • printfn support F# types better via %A specifier.

Cons:

Aside from not being able to reuse parameters as @mydogisbox mentioned, printfn-like functions are much slower than Console.WriteLine (due to using reflection); you shouldn't use the former for logging purpose.

like image 44
pad Avatar answered Sep 17 '22 18:09

pad