ld yield 1.4399560356056456 in all cases double a = 0.5; double b = 0.2; double v = Math.sin(a) + Math.pow(Math.cos(b),2); System.out.println(v); Functions F = Functions.functions; DoubleDoubleFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos)); System.out.println(f.apply(a,b)); DoubleDoubleFunction g = new DoubleDoubleFunction() { public double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); } }; System.out.println(g.apply(a,b));
Performance
Surprise. Using modern non-adaptive JITs such as SunJDK 1.2.2 (java -classic) there seems to be no or only moderate performance penalty in using function objects in a loop over traditional code in a loop. For complex nested function objects (e.g.
F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos)))) the penalty is zero, for trivial functions (e.g.
F.plus) the penalty is often acceptable.
Iteration Performance [million function evaluations per second] Pentium Pro 200 Mhz, SunJDK 1.2.2, NT, java -classic, |
| 30000000 iterations | 3000000 iterations (10 times less) | |
F.plus | a+b | F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos))) | Math.abs(Math.sin(a) + Math.pow(Math.cos(b),2)) | | |
| 10.8 | 29.6 | 0.43 | 0.35 | | |
@author wolfgang.hoschek@cern.ch
@version 1.0, 09/24/99