• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Yves's functional programming promo book

 
Ranch Hand
Posts: 1907
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Yves,
I am currently reading tutorials of Java Lambdas. Java being a imperative language, does functional programming in java causes any performance penalty ?
 
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Arjun,

Functional programming does not, by itself, imply performance penalty. There are however some reasons for performance problems that are related to specific languages. For example, Java is not recursive. It is supposed to be, but as it doesn't do Tail Call Elimination, it is limited by the size of the stack. You can increase the size of the stack, but this increased size will apply to all threads, which means a waste of memory because not all threads will need this increased size. We can solve this problem by running recursive call on the heap instead of on the stack, through the use of trampolining. One chapter of my book is dedicated to this technique. But since one object will be created for each step, it is a bit slower. It has however never been a problem for me although I use it heavily in production code.

Before Java 8, using high order functions implied creating objects, generally instances of anonymous classes. This could have a very minimal cost, but it could have a bigger one if used in loops. With Java 8 lambdas, anonymous classes no longer need to be created, although you can still force Java to create them (sometimes inadvertently).

The main reason for performance impact is using the wrong data structures. A singly linked list is much faster than an ArrayList for insertion and retrieval on one side. It is however much slower for the same operation on the other side. It is also much slower for indexed access, but much faster for multiple deletions on huge lists. But you can use other structures for indexed access (Vector) or for double end access (Double Ended Queue). But it is true that sometimes, you need a structure that is good enough for multiple types of access and may find the ArrayList more efficient. But you have to put this in balance with the numerous benefits of being immutable.
 
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While on about creating instances of anonymous classes, does the use of λs instead reduce the potential security hazard of synthetic accessor methods?
 
Pierre-Yves Saumont
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With lambdas, there is no anonymous classes, so there is no risk related to synthetic methods to private members as it was the case with anonymous classes. The main consequence is that the this reference in a lambda refers to the enclosing class. This dos not suppress all such problems, such as synthetic method created for members of parameterized types, but this is not related to lambdas, but more generally to leaking the this reference. Note however that in functional programming, member variables are always made final, so this would in any case be much less of a problem.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic