Live data from Hacker News

Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

unriskinsight.blogspot.com

91–100 of 115 posts

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#91
post #9
post #4

Neat results, and a neat comparison of the languages. I'm a bit curious how an asm.js port (via whatever means. maybe c++ -> emscripten?) ends up performing. edit: heh. > Also note that FORTRAN is not included in the list [of relative speedups], because no sane person would switch to FORTRAN from another programming language voluntarily.

It's embarrassing that Fortran gets beat by Java.

It's embarrassing that Fortran got beaten by a very unoptimized Java code. But this is probably not because of Fortran is slow and Java is fast, but because the whole benchmark results are pretty much a coincidence of bad coding and if someone else wrote it, the results could be as well completely reversed. E.g. the Java code uses some of the worst performance antipatterns that are possible to do in Java, e.g. like creating small objects everywhere (Optional), while C++ version uses primitives like ints and bools.

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#92
post #86

I was curious how a non-functional version would fare, so I wrote one in Nimrod and it's a lot faster than the functional C++: https://gist.github.com/def-/8187448ea7a5c8da8265 Goats Wolves Lions C++11 Nimrod 17 55 6 0.00 0.00 117 155 106 0.17 0.01 217 255 206 0.75 0.01 317 355 306 2.16 0.01 417 455 406 5.28 0.01 517 555 506 10.75 0.01 617 655 606 19.15 0.02 717 755 706 31.58 0.02 817 855 806 46.52 0.02 917 955 906 6…

The important word here is "functional" C++. This is not about "fastest" C++. What does your comparison tell us?

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#93
post #92
post #86

I was curious how a non-functional version would fare, so I wrote one in Nimrod and it's a lot faster than the functional C++: https://gist.github.com/def-/8187448ea7a5c8da8265 Goats Wolves Lions C++11 Nimrod 17 55 6 0.00 0.00 117 155 106 0.17 0.01 217 255 206 0.75 0.01 317 355 306 2.16 0.01 417 455 406 5.28 0.01 517 555 506 10.75 0.01 617 655 606 19.15 0.02 717 755 706 31.58 0.02 817 855 806 46.52 0.02 917 955 906 6…

The important word here is "functional" C++. This is not about "fastest" C++. What does your comparison tell us?

That functional might not be the best damned paradigm on the planet for every use case? :)

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#94
post #51

But C++11 solution is not functional. It uses state variables: look at the while loop, it updates variable. Look at the next_forests.reserve, std::back_inserter. These are not clear functional constructs. Of course it is possible to model memory state with the monads :) but... The C++ code is quite different from other codes. So i was surprised by Java which is only 3 times slower than C++ while running a much less e…

Glad someone pointed that out. It is cheating. It is doing a lot of state transformations like in-place sorting / filtering. I'd like to see some real C++ functional code here, using immutable data structures, not an imperative program using lambdas. I guess it would be both much harder to write (C++ is not really a functional language) and much slower, because dynamic memory allocation in C++ is more costly than in…

Isn't this just evidence of how broad of a definition "functional" programming is? It is the scottsman of programming debates. (Though, I suspect any "paradigm" example will fall into this trap.)

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#95
post #75
post #49

Earlier quoted context omitted.

Cliff Click, a smart man in whom I see many admirable qualities, than whom there are few more complete experts on the JVM, gave a tech talk at Facebook about how Java is faster than C++ just a few months ago. Tons of perfectly smart engineers sat and took it seriously. Much of the content was a rehash of this older discussion: http://www.azulsystems.com/blog/cliff/2009-09-06-java-vs-c-p... There are plenty of serious…

I know a professor who worked on HotSpot Java VM in early 2000s. He is a smart man, but he always makes these false claims how Java will be as fast as C++ the next years. He was the boss of a local Oracle Labs company, though he left that company recently and is now an C# advocate and make similar claims in favor of MS. Such persons are quite annoying as they try to influence a lot of students.

Agree. Some kind of group hope, a part of issue w/ the Java lang. Data points in the other direction: http://benchmarksgame.alioth.debian.org/u32q/java.php

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#96
post #29
post #11

Earlier quoted context omitted.

Java's performance relative to other commonly used languages is very good. It's basically been at 2-3x slower than C/C++ for quite a while.

2-3x slower? I guess it depends on what you're doing. For a lot of code I've dealt with the differences in performance have been factional.

This doesn't make much sense, unless:

1) You wrote that code in BOTH Java and C++, optimized both, and compared their running times.

2) Said code was CPU bound. For heavy IO bound code you could even use TCL and get "fractional" differences.

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#97
post #8
post #2

It's pretty astonishing how easily C++ trounces everything else including Java. I guess there's still something to be said for compiling directly to optimised binaries.

Supposedly, Java is going to be faster than native code any day now. It's been said for years. The case was somewhat credible at one time, because the opportunity exists to optimize using runtime information. I think the reason it didn't go that way is: 1. CPUs have gotten very good at doing runtime optimization kinds of things on their own, like predicting branches and reducing the cost of virtual function calls. 2.…

People don't use Java because it's fast.

They use Java because it's fast enough, easy, safe, reliable, easy to instrument, easy to debug, has wonderful tool support, is mature, has many tested and heavily used frameworks, has a free chunk of code to do anything you can imagine you can just snag via maven, integrates with everything, works on all major platforms and costs nothing.

That makes it fast in other ways.

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#98

SBCL or at least Haskell by any chance? And then in terms of lines of code.)

Here's a Haskell translation of the C++ version. Runtume is around 10x of the original (probably vector vs list cache trashing and vector sort/unique vs list to set/set to list), code reduction is 3x.

  import qualified Data.Set as S                                                                                                                                                
  
  data Forest = F Int Int Int
    deriving (Eq, Ord, Show)
  
  meal forests = (S.toList . S.fromList)
    [nextForest |
     forest  meal,
     valid nextForest]
   where
    possibleMeals = [
      F (-1) (-1)   1,
      F (-1)   1  (-1),
      F   1  (-1) (-1)]
    F x y z  F x' y' z' = F (x+x') (y+y') (z+z')
    valid (F x y z) = x >= 0 && y >= 0 && z >= 0
  
  findStable forest = iter [forest]
   where
    iter forests | not (done forests) = iter (meal forests)
                 | otherwise          = filter stable forests
    done forests = null forests || any stable forests
    stable (F _ 0 0) = True
    stable (F 0 _ 0) = True
    stable (F 0 0 _) = True
    stable _         = False
  
  main = print $ findStable (F 117 155 106)

Re: Performance comparison of Functional C++ 11, Java 8, JavaScript and Wolfram

#100
Here is my natural (without any extra effort thinking about speed) F# solution: (for calibration I have the C++, unchanged, times)

   | 217 | 255 | 206 | 0.5 s (C++ 1.6s)
   | 317 | 355 | 306 | 1 s   (C++ 4.8s)
   | 617 | 655 | 606 | 3.5 s (C++ 35s)
   | 917 | 955 | 906 | 10s   (C++ 117.5)
code:

    let actions = [|[|-1; -1; 1|]; [|-1;1;-1|]; [|1; -1;-1|]|]

    let stable [| g ; w; l|] = (g = 0 && (w = 0 || l = 0)) || (w = 0 && l = 0)

    let isSound = function | [| x ; _; _|] | [|_; x; _|] | [|_; _; x|] when x  false | _ -> true

    let stateChange state =  Array.map (Array.map2 (+) state) actions 

    let deduplicate sequence = sequence |> Seq.groupBy hash |> Seq.map (snd >> Seq.head)

    let forest start =
      let rec search curforest =  
            let nextforest = Array.collect stateChange curforest
                                      |> Array.filter isSound 
                                      |> deduplicate 
                                      |> Seq.toArray  
                                
            if nextforest.Length = 0 then curforest 
            else match Array.tryFind stable nextforest with 
                    | Some _ -> nextforest
                    | None -> search nextforest

      search (stateChange start) |> Array.filter stable
Post reply on HN