r/scala • • Jun 18 '14

Scala is faster than java

http://jto.github.io/articles/scala_is_faster_than_java/
4 Upvotes

23 comments sorted by

18

u/thatsIch Jun 18 '14

Scala [...] libraries [...] have better performances than their Java counterparts.

Might be a more fitting header

14

u/fabienbk Jun 18 '14 edited Jun 18 '14

Some scala libraries have better performance than their java counterparts, under certain circumstances

It's hilarious that it became "scala is faster than java".

14

u/balefrost Jun 18 '14

The author even points this out in his article. I think the title was clickbait, and the title was copied verbatim here.

1

u/[deleted] Jun 18 '14

I'm so used to clickbait I hardly even notice it anymore. By the time I've read the conclusion I've forgotten the title.

The author clearly meant to debunk the myth that Scala is "always" slower than Java, which I think is a fair discussion to have given the seemingly common stigma.

2

u/mandubian Jun 18 '14

I don't think the subject of this article is perf :)

1

u/thatsIch Jun 18 '14

Yea, but I cant quote it like this, since its not written like this ^ ^

1

u/gregK Jun 18 '14

1

u/oelang Jun 18 '14

I'm not surprised, java's streams are (manually) specialized so they don't box. Scala views are broken, unmaintained and very slow especially for numerics.

This article could have been a lot more interesting if it compared lambdas to imperative code because even java streams have a lot of overhead.

Btw, it's possible to use java 8 streams in scala 2.11, SAM support is still experimental though.

-2

u/[deleted] Jun 18 '14

At least the Java code is easier to read. I still don't understand who thought that Play's current JSON validation API was a good idea.

6

u/skaalf Jun 18 '14

The Scala code is fairly simple IMHO. I think your problem is more familiarity than complexity (btw Rich Hickey made an excellent talk about this called "Simple made easy").

You can read it line by line (see comment):

val carValidation = From[JsValue]{ __ =>        // I'm working on Json 
    ((__ \ "manufacturer").read[String] ~          // It contains a String called "manufacturer"
     (__ \ "seatCount").read(min(2)) ~              // It contains a Int called "seatCount", the minimal valid value is 2
     (__ \ "licensePlate").read(minLength(2) |+| maxLength(14)))(Car.apply _) // It contains a String called "licensePlate", the minimal valid length is 2, the maximal valid length is 14
}

The API also gives you excellent feedback in case of errors (cause, and location in the Json). The feedback in Java is, by comparaison, poor.

2

u/[deleted] Jun 18 '14 edited Jun 19 '14

I can read the code just fine. The problem is when someone unfamiliar with the Play validation API has to do something. It's pretty much impossible to get anything done without reading through the (horrible) documentation, whereas the JSR-303 API is just about simple as it gets (annotate the field with whatever validation you need and run it through a Validator, or if you're using Jersey, just add the @javax.validation.Valid annotation to your method).

1

u/skaalf Jun 18 '14

While I get your point, I'd like to point out that this operator is not validation specific, and is actually applicative composition. You can also use and if you don't like it.

The API here is not the Json API, but an evolution designed to feel similar to Play users. It can also validate Form data, or pretty much anything (see: http://jto.github.io/articles/play_new_validation_api/).

To be fair, the Json API documentation was completely rewritten, and is a lot better than it used to be.

2

u/jlh276 Jun 23 '14

whereas the JSR-303 API is just about simple as it gets

I don't know anything about JSR-303 and I had a glance at an example:

public class Address {
    @NotEmpty(groups = Minimal.class)
    @Size(max=50, groups=FirstStep.class)
    private String street1;
    
    @NotEmpty(groups=SecondStep.class)
    private String city;

    @NotEmpty(groups = {Minimal.class, SecondStep.class})
    private String zipCode;
    ...

    public interface FirstStep {}

    public interface SecondStep {}
    
    @GroupSequence({Firststep.class, SecondStep.class})
    public interface Total {}
}

Well, I have no clue what groups, GroupSequence do and in addition to the documentation it feels I'll have to go look at Minimal, FirstStep, SecondStep as well as every place they are used to understand what the validation does. I think annotations for validation is a terrible idea, I can see immediately with the code above that it will be challenging to maintain separate validations rules depending on the context. It seems JSR-303 appear simple. Whether it really is, I have my doubts.

6

u/Ukonu Jun 18 '14

You're right. It's definitely an issue of familiarity over complexity.

However, the main problem with that is when every library writer thinks their library is important enough that I should become familiar with their particular usage of operator overloading. People don't want to and I don't blame them. I'd rather you just use slightly more verbose method names so that I can read your code quickly, make reasonable assumptions about what's happening, and get on with my life. I tell you all this as a fan of operator overloading, because, when used well, it can truly help build more intuitive APIs.

Code is read far more than it is written. Every good engineer should be empathetic and always aim to make their code as readable as possible even if it means an increase in keystrokes.

1

u/pathikrit Jun 18 '14

Also, try this library: https://github.com/pathikrit/dijon

2

u/skaalf Jun 18 '14

And say goodbye to typesafety.

5

u/void_fraction Jun 18 '14

I find the Play code much more readable, and, more importantly, concise. I dislike their use of custom operators, but it's not anything too esoteric.

1

u/skaalf Jun 18 '14

If you don't like the ~, you can use "and" instead

2

u/onmach Jun 18 '14

Good god, java code is horrendous. It is by far the least readable non joke language I've been exposed to.

2

u/[deleted] Jun 18 '14

what side of the line is perl on when it comes to the joke/non-joke language line?

1

u/mancvso Jun 18 '14

I want to know that too.

1

u/onmach Jun 19 '14

I've seen some decent software in perl, but I've also seen the worst code imaginable in perl. I don't code in it enough to say why.

1

u/pumpkinpumpkinBUTT Jun 19 '14

perl yearns for me to be clever, which is almost always the wrong decision