Posts

Elixir: first look

I don't have a clear impression about Elixir language yet. I don't like it has Ruby like syntax, but do like it has pipe operator and macros. So, Fibonacci: It executes in about 13 seconds which is on pair (even faster for unknown reason) with Erlang, no surprises here. D (GDC) - 0.990 C# - 1.26 D (DMD) - 1.3 C++ - 1.33 F# - 1.38 Nemerle - 1.45 Rust - 1.66 Go - 2.38 Haskell - 2.8 Clojure - 9 Elixir - 13 Erlang - 17 Ruby - 60 Python - 120

SHA1 compile time checked literals: F# vs Nemerle vs D

Image
I've always been interested in metaprogramming. Sooner or later, I'm starting to feel constrained within a language without it. F# is a really nice language, but I'm afraid I'd have got bored with it if it'd not have Type Providers, for example. Why metaprogramming is so important? Because it allows changing a language without cracking the compiler. It allows making things which seemed to be impossible to implement. I'm dealing with cryptography hashes a lot at work, nothing rocket since, just MD5, SHA-1 and so on. And I write tons of tests where such hashes are used in form of string literals, like this: The problem with this code is that the compiler cannot guarantee that the hex string in the last line represents a valid SHA-1. If it does not, the test will fail at runtime for a reason it's not intended to. OK, now we can formulate our task: provide a language construct to enforce a string literal being a valid SHA-1 hexadecimal, at compile t...

Fib: C++, C# and GDC

As a reference implementation, I added C++ one: It's execution time is 1.33 seconds, which surprisingly is not the best result so far. A C# version: Also, I compiled  this D code with GDC compiler and it executed in 990 ms, which is the best result: D (GDC) - 0.990 C# - 1.26 D (DMD) - 1.3 C++ - 1.33 F# - 1.38 Nemerle - 1.45 Rust - 1.66 Go - 2.38 Haskell - 2.8 Clojure - 9 Erlang - 17 Ruby - 60 Python - 120 Unfortunately, I have not managed to compile the D code with LDC compiler, it returns the following error: Building: DFib (Release) Performing main compilation... Current dictionary: d:\git\DFib\DFib D:\ldc2-0.15.2-beta1-win64-msvc\bin\ldc2.exe -O3 -release "main.d"   "-od=obj\Release" "-of=d:\git\DFib\DFib\bin\Release\DFib.exe" LINK : fatal error LNK1181: cannot open input file 'kernel32.lib' Error: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe failed with status: 1181 ...

Composing custom error types in F#

I strongly believe that we should keep code as referential transparent as possible. Unfortunately, F# language does not encourage programmers to use Either monad to deal with errors. The common practice in the community is using common in the rest .NET (imperative) world exception based approach. From my experienced, almost all bugs found in production are caused by unhandled exceptions.  The problem In our project we've used the Either monad for error handling with great success for about two years. ExtCore  is a great library making dealing with Either, Reader, State and other monads and their combinations really easy. Consider a typical error handling code, which make use Choice computation expression from ExtCore: The code is a bit hairy because of explicit error mapping. We could introduce an operator as a synonym for Choice.mapError, like <!>, after which the code could become a bit cleaner: (actually it's the approach we use at in our tea...

Go: fib

Go code is relatively low-level since it does not have "foreach over range" syntax construct: Results are not as impressive for a systems language: 2.38 seconds. And it lays below Rust but under Haskell: C# - 1.26 D (DMD) - 1.3 F# - 1.38 Nemerle - 1.45 Rust - 1.66 Go - 2.38 Haskell - 2.8 Clojure - 9 Erlang - 17 Ruby - 60 Python - 120

Computing cryptography hashes: Rust, F#, D and Scala

Image
Let's compare how fast Rust, D and F# (.NET actually) at computing cryptography hashes, namely MD5, SHA1, SHA256 and SHA512. We're going to use rust-crypto cargo : Results: MD5 - 3.39s  SHA1 - 2.89s  SHA256 - 6.97s SHA512 - 4.47s Now the F# code: Results (.NET 4.5, VS 2013, F# 3.1): MD5CryptoServiceProvider - 2.32s (32% faster) SHA1CryptoServiceProvider - 2.92s (1% slower) SHA256Managed - 16.50s (236% slower) SHA256CryptoServiceProvider - 11.50s (164% slower) SHA256Cng - 11.71s (168% slower) SHA512Managed - 61.04s (1365% slower) SHA512CryptoServiceProvider - 21.88s (489% slower) SHA512Cng - 22.19s (496% slower) (.NET 4.6, VS 2015, F# 4.0): MD5CryptoServiceProvider elapled 2.55 SHA1CryptoServiceProvider elapled 2.89 SHA256Managed elapled 17.01 SHA256CryptoServiceProvider elapled 8.74 SHA256Cng elapled 8.75 SHA512Managed elapled 23.42 SHA512CryptoServiceProvider 5.81 SHA512Cng elapled 5.79 D: DMD MD5 - 16.05s (470% ...

Rust: fib

Rust is an interesting language. It is not a primitive one, like Go where we don't have ADTs, pattern matching and generics (but we do have Nils). And it's advertising as a safe and performant system language. Today is the very first day I'm looking at it. Let's "smoke" test it with Fibonacci :) Debug: 3.44 seconds, release: 1.66 seconds. This is not very impressive, but pretty fast indeed. C# - 1.26 D (DMD) - 1.3 F# - 1.38 Nemerle - 1.45 Rust - 1.66 Haskell - 2.8 Clojure - 9 Erlang - 17 Ruby - 60 Python - 120 It's very interesting how it'll behave in concurrent Fibonacci test. The compiler is quite slow: it takes 2-3 seconds to build this tiny program.