I developed a module which should work as a set of Java Stored Procedures. While developing, I used it as stand-alone application with main() used to run unit tests.
While doing this, I took notice that routines called in Oracle works significantly slower, that same routines used in stand-alone application. Just for example, HashMap.get is about to four time slower, socket streams in/out is about to three times slower, socket creating and accepting is much slower and so on.
I developed a simple test case. I got a computer with Oracle Database 12c, PL/SQL Developer and IDEA running. In IDEA I ran this code:
import java.util.*;
public class SpeedTest {
public static String execute() {
long start = System.currentTimeMillis();
String s = "";
Object k = new Object();
Map m = new HashMap();
for (int i = 0; i < 100000; i++) {
s += Integer.toString(i);
m.put(k, s);
}
long finish = System.currentTimeMillis();
long delta = finish - start;
return "Time = " + Double.toString(delta/1000);
}
public static void main(String[] args) {
System.out.println(execute());
System.out.println(execute());
System.out.println(execute());
}
}
It's results:
Time = 18.0
Time = 18.0
Time = 18.0
Process finished with exit code 0
Then I ran the same example into Oracle:
create and compile java source named "SpeedTest_Source" as
import java.util.*;
public class SpeedTest {
public static String execute() {
long start = System.currentTimeMillis();
String s = "";
Object k = new Object();
Map m = new HashMap();
for (int i = 0; i < 100000; i++) {
s += Integer.toString(i);
m.put(k, s);
}
long finish = System.currentTimeMillis();
long delta = finish - start;
return "Time = " + Double.toString(delta/1000);
}
public static void main(String[] args) {
System.out.println(execute());
}
}
/
create function SpeedTest_Execute return varchar2 as
language java name 'SpeedTest.execute() return java.lang.String';
/
set serveroutput on
exec dbms_output.put_line(SpeedTest_Execute);
exec dbms_output.put_line(SpeedTest_Execute);
exec dbms_output.put_line(SpeedTest_Execute);
It's results:
Time = 44.0
Time = 63.0
Time = 88.0
Yes, it's not only slower, but slowness is growing. I guess it's not correct. Am I wrong? Can I do something to make stored java procedure as fast as stand-alone one?