3 ms·
Worth checking out Common Lisp. It's as high-level language as you can get, and yet good compilers (like SBCL, or like commercial ones from Franz and LispWorks)
by TeMPOraL 10y ago
Worth checking out Common Lisp. It's as high-level language as you can get, and yet good compilers (like SBCL, or like commercial ones from Franz and LispWorks) can compile it to tight assembly with performance very close to that of C++ (you need to disable some runtime checks for that though, but you can do that on a per-function level, so it's much less of a problem than one thinks).
- runeks 10y agoHaskell is also really fast. On most Stack Overflow questions asking "why is this Haskell program slow", there's often a detailed answer with an implemention that's about as fast as -- or even faster than -- C: http://stackoverflow.com/questions/42771348/why-is-haskell-so-slow-compared-to-c-for-fibonacci-sequence http://stackoverflow.com/questions/42771348/why-is-haskell-s... http://stackoverflow.com/questions/6964392/speed-comparison-with-project-euler-c-vs-python-vs-erlang-vs-haskell http://stackoverflow.com/questions/6964392/speed-comparison-... http://stackoverflow.com/questions/29875886/haskell-why-does-int-performs-worse-than-word64-and-why-my-program-is-far-slow http://stackoverflow.com/questions/29875886/haskell-why-does...
- Johnny_Brahms 10y agoNone of those are very good examples. They spend a lot of time optimizing already dead-slow algorithms, and they end up with an optimized dead-slow haskell version that is faster than the non-optimized dead-slow C version. Not very impressive. I thought I would implement some naive versions: For he second link, I just whipped up this naive in chez scheme, and it runs in 0.5s, and that should really be the baseline: https://pastebin.com/JXGLA4TR https://pastebin.com/JXGLA4TR Incidentally this seems to be on par with the fastest haskell version posted that does not use precomputed primes for factorisation. It could be made a lot faster by using only primes, but I couldn't be bothered. And I doubt you will find a haskell version that gets the longest collatz-sequence that is faster than this: https://pastebin.com/FAdiHA3X https://pastebin.com/FAdiHA3X (0.01s using gcc -O2). Yet again, a simple algorithm, but this time with bolted-on memoization. Edit: The first link contains the most naive and slow fibonacci function. It might be mathematically elegant, but it is dirt slow. (define (fib n) (let loop ([n n] [a 0] [b 1]) (if (zero? n) b (loop (- n 1) b (+ a b))))) That one takes 0.1s calculating the 100.000th fibonacci number, and there are even faster ways.
- kgabis 10y agoBut you still don't get any control over memory layout of your data and how allocations are performed and this could easily cost you an order of magnitude in performance.
- TeMPOraL 10y agoTrue, at least without using any vendor-specific extensions (that may or may not help here; I haven't investigated). My point was that in CL, you get close-to-C++ performance levels by default, you can get even closer by optimizing specific areas in your code, and all of that while still writing in a very high-level language. Of course you won't beat memory-optimized C++ code in performance, but you're already close enough that you rarely need to.