List manipulating performance

2016-07-24

A few weeks ago we’ve got a little discussion about “Which way should we use to manipulate list or collection?” at the office.

To get the answer I created a tiny sample project to compare each library’s performance.

In this time I chose a few libraries, Lightweight-Stream-API(Java8 backport), RxJava(Reactive Extensions for Java), IxJava(Interactive Extensions for Java).

With above libraries I prepared a simple processing like:

  • Put string list(has 20 item) to method
  • Fiter them by having a “ok” prefix or not
  • Change them to upper case letter
  • Return converted list

The actual code is the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// pure implementation
static List<string> filter(List<string> arg) {
List<string> result = new ArrayList();
for (String str : arg) {
if (str != null && str.startsWith("ok")) {
result.add(str.toUpperCase());
}
}
return result;
}
static List<string> filterStream(List<string> arg) {
return Stream.of(arg)
.filter(value -> value.startsWith("ok"))
.map(String::toUpperCase)
.collect(Collectors.toList());
}
static List<string> filterRx(List<string> arg) {
return Observable.from(arg)
.filter(value -> value.startsWith("ok"))
.map(String::toUpperCase)
.toList()
.toBlocking()
.single();
}
static List<string> filterIx(List<string> arg) {
return Ix.from(arg)
.filter(value -> value.startsWith("ok"))
.map(String::toUpperCase)
.toList()
.single();
}

Result

Actualy I timed each method execution by JUnit(not on the Android device, just on my local macbook with Java8(1.8.0_77).

You can see the test code from here.

And the result and average times(from 5 times execution) are…

  • With no libraries…1ms
  • Lightweight-Stream-API…34ms
  • RxJava…136ms
  • Ixjava…270ms

Conclusion

  • When it comes to the performance, using no library is the fastest as we expected, but the code becomes a little bit tedious.
  • If we choose one library, It seems like Lightweight-Stream-API is the best selection, since it has a good performance and syantax is just the backport of Java8.
  • If there is no specific reason I don’t recommend you to use RxJava and IxJava to deal with this kind of problem. The syantax is sort of tedious and performance is not good either.

You can see my sample from here, so please give me a feedback if you have one.