1, speedup function(method) call

public class TestQuickFunctionCall extends TestCase {

        public static interface IQuickIdCallObject {
                public Object execIdCall(int methodId, Context cx, Scriptable 
scope,
                                Scriptable thisObj, Object[] args);
        }
        public static class QuickIdCallFunction extends ScriptableObject
implements
                        Function {
                private static final long serialVersionUID = 1L;

                IQuickIdCallObject obj;
                int methodId;

                public QuickIdCallFunction(IQuickIdCallObject obj, int 
methodId) {
                        this.obj = obj;
                        this.methodId = methodId;
                }

                @Override
                public String getClassName() {
                        return "QuickIdCallFunction";
                }

                @Override
                public Object call(Context cx, Scriptable scope, Scriptable 
thisObj,
                                Object[] args) {
                        return obj.execIdCall(methodId, cx, scope, thisObj, 
args);
                }

                @Override
                public Scriptable construct(Context cx, Scriptable scope, 
Object[]
args) {
                        throw new RuntimeException("this function is not a 
constructor!");
                }

        }
        public static class ObjectA extends ScriptableObject implements
                        IQuickIdCallObject {
                private static final long serialVersionUID = 1L;

                final static int ID_sqrt = 0, ID_square = 1;
                final static Map<String, Integer> nameToId = new HashMap<String,
Integer>();
                static {
                        nameToId.put("sqrt", ID_sqrt);
                        nameToId.put("square", ID_square);
                }

                public ObjectA() {

                }

                public Object execIdCall(int methodId, Context cx, Scriptable 
scope,
                                Scriptable thisObj, Object[] args) {
                        Object o = args[0];
                        switch (methodId) {
                        case ID_sqrt:
                                return Math.sqrt(((Number) o).doubleValue());
                        case ID_square:
                                double d = ((Number) o).doubleValue();
                                return d * d;

                        default:
                                throw new RuntimeException("function not 
support! index="
                                                + methodId);
                        }
                }

                @Override
                public String getClassName() {
                        return "ObjectA";
                }

                @Override
                public Object get(String name, Scriptable start) {
                        Integer id = nameToId.get(name);
                        if (id != null)
                                return new QuickIdCallFunction(this, id);

                        return null;
                }

        }

        public void testQuickFuncion() throws Exception {
                Context context = Context.enter();

                ScriptableObject scriptObject = (ScriptableObject) context
                                .initStandardObjects();
                ScriptableObject.defineClass(scriptObject, ObjectA.class);

//              context.setOptimizationLevel(9);
                Script script = context.compileString(
                                "a = new ObjectA(); for(i=0;i<1000000;i++){str 
= a.sqrt(10) +
a.square(10);}", null, 1, null);

                long before = System.nanoTime();
                Object ret = script.exec(context, scriptObject);
                System.out.println("js func call, time used: " + 
(System.nanoTime()
- before) / 1000000 + "ms");

                ObjectA obj = new ObjectA();
                before = System.nanoTime();
                double t = 0;
                Object[] ps = new Object[1];
                for(int index=0;index<1000000;index++)
                {
                        ps[0] = 10;
                        t += ((Number)obj.execIdCall(0, null, null, null, 
ps)).doubleValue
();
                        t += ((Number)obj.execIdCall(1, null, null, null, 
ps)).doubleValue
();
                }
                System.out.println("java func call, time used: " + 
(System.nanoTime
() - before) / 1000000 + "ms");

                Context.exit();
        }
}

using vm argument: -server
the result is:
js func call, time used: 940ms
java func call, time used: 64ms


can someone tell me how to speed it up?

2, string operation is really slow!
try this script:
a = new ObjectA(); for(i=0;i<1000000;i++){str = a.sqrt(10) + ' ' +
a.square(10);
result in:
js func call, time used: 22294ms
java func call, time used: 32ms
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to