Package org.reactfx.inhibeans.demo

Source Code of org.reactfx.inhibeans.demo.FibTest

package org.reactfx.inhibeans.demo;

import javafx.beans.binding.NumberExpression;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;

import org.reactfx.Guard;

public class FibTest {

    private final int N;
    private final NumberExpression[] fib;

    private long invalidationCount = 0;

    private FibTest(int n) {
        N = n;
        fib = new NumberExpression[N];
        fib[0] = new SimpleLongProperty(0);
        fib[1] = new SimpleLongProperty(0);
        for(int i = 2; i < N; ++i) {
            fib[i] = fib[i-2].add(fib[i-1]);
        }
    }

    public void setupFor(LongProperty resultHolder) {
        resultHolder.bind(fib[N-2].add(fib[N-1]));

        // count the invalidations of the result
        resultHolder.addListener(o -> {
            invalidationCount += 1;
            resultHolder.get(); // force recomputation
        });
    }

    public void run() {
        ((LongProperty) fib[1]).set(1);
    }

    public static void main(String[] args) {
        int n = 40;

        FibTest eagerTest = new FibTest(n);
        LongProperty eagerResult = new SimpleLongProperty();
        eagerTest.setupFor(eagerResult);

        FibTest lazyTest = new FibTest(n);
        org.reactfx.inhibeans.property.SimpleLongProperty lazyResult =
                new org.reactfx.inhibeans.property.SimpleLongProperty();
        lazyTest.setupFor(lazyResult);

        long t1 = System.currentTimeMillis();
        eagerTest.run();
        long t2 = System.currentTimeMillis();
        double eagerTime = (t2-t1)/1000.0;

        t1 = System.currentTimeMillis();
        Guard g = lazyResult.block();
        lazyTest.run();
        g.close();
        t2 = System.currentTimeMillis();
        double lazyTime = (t2-t1)/1000.0;

        System.out.println("EAGER TEST:");
        System.out.println("    fib_" + n + " = " + eagerResult.get());
        System.out.println("    result invalidations: " + eagerTest.invalidationCount);
        System.out.println("    duration: " + eagerTime + " seconds");
        System.out.println();
        System.out.println("LAZY TEST:");
        System.out.println("    fib_" + n + " = " + lazyResult.get());
        System.out.println("    result invalidations: " + lazyTest.invalidationCount);
        System.out.println("    duration: " + lazyTime + " seconds");
    }
}
TOP

Related Classes of org.reactfx.inhibeans.demo.FibTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.