-
Notifications
You must be signed in to change notification settings - Fork 0
Grid all paths problem
David Hofer edited this page Apr 14, 2016
·
15 revisions
Today I saw a post describing a "count all paths" problem and solutions to it written in Ruby, C, and Go: http://rayhightower.com/blog/2016/04/11/comparing-ruby-c-and-go/
I decided to try a (one-liner) solution in Crystal: https://github.com/dkhofer/sandbox/blob/master/crystal/all_paths.cr
The payoff:
$ crystal build crystal/all_paths.cr --release
$ time ./all_paths
184756
real 0m0.012s
user 0m0.007s
sys 0m0.004s
This is very nearly as fast as the C code! I think this example shows, at least for problems of this scale, that the syntax of Go and C is not what makes them superior in performance to Ruby. It's mainly "just" that they have compilers. Well, and a popcount function, which Crystal has but Ruby does not.
To confirm this was a fair comparison, I ran the Ruby, C, and Go code on my laptop too.
Ruby:
$ time ruby/all_paths.rb
184756
real 0m3.942s
user 0m3.754s
sys 0m0.056s
C:
$ gcc all_paths.c -o all_paths
$ time ./all_paths
184756
real 0m0.010s
user 0m0.007s
sys 0m0.002s
Go:
$ go get github.com/cznic/mathutil
$ go build ./all_paths.go
$ time ./all_paths
184756
real 0m0.013s
user 0m0.008s
sys 0m0.004s