Hi,

I observed a real dramatic difference in performace when using the
following two ways to get the same result.

(bind ?value (?stringObject codePointAt 0))
(bind ?value (Helper.stringCodePointAt ?stringObject 0))

I did some not so scientific measurements with the following clp:

(deftemplate model1 (slot a)(slot b))
(deftemplate model2 (slot a)(slot b))

(import Helper)

(deffunction test1 (?string)
        (bind ?start (System.nanoTime))
        (bind ?var (?string codePointAt 0))
        (bind ?stop (System.nanoTime))
        (printout t ">> test1 took in nanosec: " (- ?stop ?start) crlf)
)

(deffunction test2 (?string)
        (bind ?start (System.nanoTime))
        (bind ?var (Helper.stringCodePointAt ?string 0))
        (bind ?stop (System.nanoTime))
        (printout t "-- test2 took in nanosec: " (- ?stop ?start) crlf)
)

(deffunction compare (?s1 ?s2 ?message)
        ;(printout t ?s1 ":" ?s2 " - called by " ?message " returns "
(eq ?s1 ?s2) crlf)
        (if (eq ?s1 ?s2) then
                (test1 a)
                (test2 a))
        (return (eq ?s1 ?s2)))

(defrule rule1
        (model1(a ?model1a &:(compare ?model1a a "model1 slot a"))(b
?model1b &:(compare ?model1b b "rule1 model1 slot b")))
        (model2(a ?model2a &:(compare ?model2a 1 "model2 slot a"))(b
?model2b &:(compare ?model2b 2 "rule1 model2 slot b")))
        (test (compare a a a))
        =>
)

.................

(defrule rule16
        (model1(a ?model1a &:(compare ?model1a a "model1 slot a"))(b
?model1b &:(compare ?model1b b "rule16 model1 slot b")))
        (model2(a ?model2a &:(compare ?model2a 1 "model2 slot a"))(b
?model2b &:(compare ?model2b 2 "rule16 model2 slot b")))
        (test (compare a a a))
        =>
)

(assert (model2 (a 1)(b 2)))
(assert (model1 (a a)(b b))) 

The difference drastic in favor of the static implementation. The times
are in the 30 to 40 microseconds while the other calls take much longer,
usually in the 1000 to 2000 microseconds.
Without rules, the difference is barely noticable.
The side effect of the performance drop has a significant impact of
scalability in a multi-cpu system.
In our test environment, using Jmeter with multiple virtual users, the
cpu usage of a 4 CPU system barely reach the 40% mark with the 'slow'
method.
With the static implementation, it can go up to the mid 90s for cpu
usage.

Any feedback is appreciated.


Son Nguyen




Reply via email to