But I just did a comparison, and it runs ~27 times faster in C then it does in Java.
I'd be willing to bet money that one or all of the following is true:
* You have hotspot disabled (or have a debugger attached) during the Java sample
* You are compiling the C code to 64-bit machine code, and comparing it to a 32-bit JVM
* You are not logging in your C code, or if you are, you are using stdout instead of cout
If none of these are the case, then you have something seriously wrong with your Java configuration. Hotspot transforms the Java bytecode to machine code that should run similarly to the machine code generated by the c compiler - since you're not allocating any objects in either case (all your variables are primitives on the stack), the gc vs malloc argument doesn't apply.
That said, hotspot is not as good at optimization as GCC is (which I'm assuming you're using; correct me if I'm wrong), so you'll still find that the C code will run marginally faster, by not by an order of magnitude. In my preliminary tests, it's somewhere between 5% and 30% slower than GCC-generated machine code.
This will have no bearing if you're not logging, but System.out is a stream, so it's not fair to compare it to stdout; use cout in your C code for a fair comparison.