2

I have a methods that prints the name of a person from a given list if it has the indicated age. This method is implemented using the imperative and then functional approach.

public static void printPerson(int age) {
    for(Person p: list) {
        if(p.age == age) {
            System.out.println(p.name)
        }
    }
}

Functional approach:

public static void printPerson(int age) {
    list.stream()
        .filter(p -> p.age == age)
        .forEach(p -> System.out.println(p.name));
}

The question is, besides readability, how else we can compare these two approaches and what would be the evaluation for each of those attributes. For example, which one has higher memory footprint, which one introduces least overhead, or has a higher response time. What other attributes (i.e., non-functional requirements) can be discussed?

Andrei
  • 7,509
  • 7
  • 32
  • 63
  • Looks like C++, are you sure you want to use the "Java" tag? – bakoyaro Jul 13 '15 at 21:39
  • @bakoyaro the last code compiles since Java 8 as [lambda expressions](http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html). – Luiggi Mendoza Jul 13 '15 at 21:44
  • [This post](http://stackoverflow.com/a/22670380/4216641) might be of interest to the question. – Turing85 Jul 13 '15 at 21:52
  • The plain loop is better in almost all aspects. At this time, Stream is definitely being abused, because it's a shiny new toy. – ZhongYu Jul 13 '15 at 22:17
  • Both are equally bad mixing separating the elements matching with side effects. If you split these up in getPersonsByAge, printPersons and a in place replacement for your method printPersonsByAge you'll see that the streams version would be small, use less memory and start producing output right away while in your imperative version need to make collections with anwer to be passed from one to the other making higher memory usage and you won't see output until all elements are filtered. – Sylwester Jul 14 '15 at 00:42

2 Answers2

2

In practice, it doesn't matter, but you'll have a higher memory footprint and CPU use with the functional approach because lambdas are relatively expensive compared to the imperative approach. The lambdas are actually just anonymous inner classes, so you're depending on the JVM to optimize away the method calls to look like the imperative version. The thing to remember about imperative programming is it looks a lot like assembly, so it tends to run fast with little extra work.

In the future, some operations like map could be parallelized, so it's somewhat future-proof.

Be warned, though: some things are a lot cleaner imperatively, but if you get too focused on functional programming, you might miss that solution. There are a lot of questions on Stackoverflow with "How do I do x in Guava" where the answer is "why are you trying to abuse Guava to do what you can do efficiently with Java's standard library in three lines of code?

Community
  • 1
  • 1
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • Actually lambdas are not the same as anonymous classes. They are implemented differently and differs compared to an anonymous class about how they capture _`this`_. I remembered reading an answer about this, just don't remember where exactly. – Alexis C. Jul 13 '15 at 21:55
  • They could just be static, then, and I'm not sure if they're pre-instantiated or if new instances are generated each time, but you still have the inlining to deal with. – David Ehrmann Jul 13 '15 at 21:58
  • Found it :) http://programmers.stackexchange.com/questions/177879/type-inference-in-java-8/181743#181743 – Alexis C. Jul 13 '15 at 22:01
  • in this particular case of printing a list of names, parallelism is very unlikely a requirement. – ZhongYu Jul 13 '15 at 22:20
0

Difficult to answer that in Java, the functional approach performances will vary a lot with the implementation.

But generally, with functional paradigm, you remove side effects and you can prove easily (in a mathematical way) that a function do the rights things.

In terms of pure performance (especially response time), i don't think functional is faster or not than imperative paradigm. The only things that matters are :

  • The way you implements
  • The way the langage (and in Java, the JVM) is implemented.

At least, i don't recommand to use the functional paradigm in Java, lamdba expressions are young for this langage. Unless you like it.

crashxxl
  • 682
  • 4
  • 18