The language I'm implementing has bignums, and I'm wondering about how much it costs to use BigInteger for everything vs. having Integer for fixnums and BigInteger for fixnums. I'm already committed to boxing all numbers, so it seems to me that I might just as well use BigIntegers all the way down. When invoking Java methods, of course, I'll have to cast from BigInteger to Integer (or Long).
I did a little profiling, which suggested that on the 1.6 JVM the cost of using BigIntegers in the Integer range is a small constant factor, 3-4, presumably because of the sign-magnitude rather than two's complement representation. I've attached the (trivial) program, which uses autoboxing. Any thoughts? -- GMail doesn't have rotating .sigs, but you can see mine at http://www.ccil.org/~cowan/signatures --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "JVM Languages" group. To post to this group, send email to jvm-languages@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en -~----------~----~----~----~------~----~------~--~---
import java.math.BigInteger; import java.util.Random; class Foo { public static void main(String[] argv) { Random r = new Random(); int s = Integer.parseInt(argv[0]); Integer[] small = new Integer[s]; BigInteger[] big = new BigInteger[s]; for (int i = 0; i < s; i++) { int j = r.nextInt(); small[i] = j; big[i] = new BigInteger(Integer.toString(j)); } long t1 = System.currentTimeMillis(); for (int i = 0; i < s; i++) { for (int j = 0; j < s; j++) { Integer k = small[i] * small[j]; } } long t2 = System.currentTimeMillis(); for (int i = 0; i < s; i++) { for (int j = 0; j < s; j++) { BigInteger k = big[i].multiply(big[j]); } } long t3 = System.currentTimeMillis(); System.out.println((t3 - t2) / (t2 - t1)); } }