简体   繁体   中英

Java 8 parallel sorting vs Scala parallel sorting

So I was just learning new Java 8, specially lambdas and date and time api. I was comparing it with scala. My basic idea was to find the execution time difference between imperative, Stream and parallel stream. So I decided to create a Library application and do some operations like searching, filtering, sorting etc. I created a Library class with a list field called books and populated it with 1000 books. Then created a functional interface for search and did some operation in all three styles. It all worked fine. My code is:

// Functional Interface
interface Search<T> {
    public void search(T t);
}

// Library class
final Library library = new Library();
// This just creates some random book objects.
final List<Book> books = collectBooks();

final Search<List<Book>> parallelSearch = (bks) -> library.findAndPrintBooksParallel(bks);

// Parallel Operations
private void findAndPrintBooksParallel(List<Book> books) {
    books.parallelStream()
        .filter(b -> b.getAuthor().equals("J.K. Rowling"))
        .sorted((x,y) -> x.getAuthor().compareTo(y.getAuthor()))
        .map(Book::getIsbn)
        .forEach(Library::waitAndPrintRecord);
}

Now I tried to re-create the same program in scala and see whether the execution is faster or not? Surprisingly scala didn't allow me to do parallel sorting (Or may be I'm being ignorant here). My scala library is

// Again some random book objects as a list
val books = collectBooks

// Parallel operation
books.par filter(_.author == "J.K. Rowling") map (_.isdn) foreach waitAndPrint

Here the books.par gives a ParSeq. This doesn't have a sort method. Is there any way I could create a parallel sort with my books list in scala. So I could write something like:

books.par filter(_.author == "J.K. Rowling") sortWith (_.author < _.author) map (_.isdn) foreach waitAndPrint

Your help is much appreciated. Thanks.

One can implement a parallel sort in scala eg http://blog.yunglinho.com/blog/2013/03/19/parallel-external-merge-sort/ . I don't know why ParSeq doesn't offer sorting in its API. Note that there are a number of alternatives to .par , so don't necessarily assume that your results from ParSeq are representative of Scala parallelism in general. (But benchmarks are valuable and I wish you well with yours).

i am very much new to scala but as far as my understanding goes: the sort itself will never be parallel, only the conversion to and from the Java array might. sorted is provided by scala.collection.SeqLike only Try this link: GitHub.Com/Scala/Scala/blob/master/src/library/scala/collection/…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM