Different programming languages has different strength and weakness. Haskell, Swift and C are popular programming languages out there. We want to compare the speed of three languages. Many low level APIs are written in C for Haskell and Swift. It is obvious that C is the fastest one, but we want to know how much faster it compares to Haskell and Swift. C is the fastest program language out there if you don't count assembly language, but we do love Haskell and Swift too since Haskell is elegant and Swift is useful in iOS dev. I will try to compare the speed of three languages and see how they stack up against each other. We are going to find all Pythagorean Tripple such as \[ S = \{ (a, b, c) \mid a^2 + b^2 = c^2, \text{ where } 1 \leq a, b, c \leq 1000 \} \\ \]
Conclusion: C language is still the King of speed in this case. Obviously there are many ways to optimize all the codes and the result will vary. However, all the code below are pretty standard.
-- ghc -O2 t.hs && time ./t >/dev/null -- time_taken 0m4.183s -- let s = [1..1000]::[Int] let p = [(a, b, c) | a <- s, b <- s, c <- s, a^2 + b^2 == c^2]
// swiftc pythagorean_tripple.swift // time_taken=11.289597 // // swiftc -O -whole-module-optimization pythagorean_tripple.swift // time_taken=1.317325 let num = 1001 for a in 1..<num{ for b in 1..<num{ for c in 1..<num{ if a*a + b*b == c*c{ print("\(a), \(b), \(c)") } } } }
// gcc -o pythagorean_tripple pythagorean_tripple.c // time_taken=2.561056 int num = 1000; for(int a = 0; a <= num; a++){ for(int b = 0; b <= num; b++){ for(int c = 0; c <= num; b++){ printf("(%d, %d, %d) \n", a, b, c); } } }