Re: Why is Java so slow????
On Nov 20, 9:41 am, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
I would recommend identifying bottlenecks,
and putting any analysis and optimization effort,
either in environment, algorithm, communication protocol,,
implementation langugage
etc into those bottlenecks.
hi BugBear,
what I'm tyring to do is convince management that there isn't anything
we cannot do as fast in Java as in "C" and this initial exercise was
a simple example. It is more about managing the risk of being "up
against a wall" with regard to the ability to speed up any arbitrary
portion of the system.
I understand I have the JNI and native methods at my disposal as
well and this will probably be the life-saver.
FYI, I've rid my example of all references to String and it is
faster again by two. My latest version is below. Still far from
the "C" version in speed but it I think it is good enough.
Thx
Larry (a.k.a. the Java Hound)
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.util.Date;
class t1
{
static byte[] prompt = "This is line ".getBytes();
static byte[] prompt2 = "\n".getBytes();
static public void main(String[] argv)
{
int lim = new Integer(argv[0]);
int nbench = new Integer(argv[1]);
int b;
for (b=0; b < nbench; b++) {
System.err.println("Bench " + b);
Date start = new Date();
try {
mytest(lim);
}
catch ( Exception e) {
System.err.println("Exception occurred");
System.err.println(e.toString());
}
Date now = new Date();
System.err.println("Took " + ((now.getTime() -
start.getTime())/1000) + " seconds");
}
}
static public void mytest(int lim) throws Exception
{
int i;
BufferedOutputStream bos = new
BufferedOutputStream(System.out, 1000000);
DataOutputStream dos = new DataOutputStream(bos);
for (i=0; i < lim; i++) {
// byte[] ibytes = new Integer(i).toString().getBytes();
writebytes(prompt, dos);
writebytes(iconv(i), dos);
writebytes(prompt2, dos);
}
dos.flush();
}
static public void writebytes(byte[] arr, DataOutputStream dos)
throws Exception
{
int n = arr.length;
int i;
for ( i=0; i < n; i++ ) {
dos.writeByte(arr[i]);
}
}
static byte[] iconv(int i)
{
byte[] digs = new byte[20];
int ndig = 0;
while ( i >= 10 ) {
digs[ndig] = (byte) (48 + i % 10);
ndig++;
i = i / 10;
}
digs[ndig] = (byte) (48 + i);
ndig++;
byte[] result = new byte[ndig];
int dig;
int j = 0;
for (dig=ndig-1; dig>= 0; dig--) {
result[j] = digs[dig];
j++;
}
return result;
}
}