Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mork

macrumors 6502a
Original poster
Jan 9, 2009
538
29
I just wrote a small program in Objective C and in Java to compare relative speeds -- expecting xcode to be much faster, but ....

The xcode took about 5 seconds where the same Java program logic took only 1.4 seconds!

My question is whether using the xcode terminal option and typing in a program that runs within xcode is a fully compiled xcode program?

If running an xcode program in terminal mode is "fully compiled" then I'm confused how xcode could/would be so much slower than the equivalent Java program.

Each program counted from 1 to 2,000,000,000 and did a simple addition inside the loop. Both programs printed out the time before and after the loop.

Look forward to any insights.

Thanks,

-- m
 

willieva

macrumors 6502
Mar 12, 2010
274
0
I suspect you're seeing the effects of optimization. I compiled and ran the following c code:
Code:
long TOP = 2000000000;

   long i=0;

   long val = 0;
   
   for(i=0;i<TOP;++i)
      val += i;

   printf("%ld\n",val);

using gcc on the command line, the code took 5 seconds to run. The same loop in a java class took 2 seconds to run.

If the code was compiled optimized, the results are much different. Using:
gcc -O2 prog.c
the resulting code took .004s to run.

Try recompiling your code with optimization flags turned on and see what you get.
 
Last edited by a moderator:

exabytes18

macrumors 6502
Jun 14, 2006
287
0
Suburb of Chicago
Post the code.

Compilers and the JVM are extremely crafty when it comes to optimization. One of my favorites with gcc is the following code:

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
	int i, j, n = 1000000000;
	long x = 0;
	
	for(i = 0; i < n; i++) {
		for(j = 0; j < n; j++) {
			x++;
		}
	}
	
	printf("x = %ld\n", x);
	return EXIT_SUCCESS;
}

The JVM apparently isn't able to reduce that code like gcc, but it can do other optimization. Hot code paths will get compiled into machine code, with proper branch predictions that gcc and other compilers could miss out on without proper runtime profile data.

Long story short: determining why one if faster than the other can be quite difficult.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Java isn't slow. Java is compiled, too. Yes, C is closer to the hardware, but what are you trying to do? Will there be a huge performance difference? Are you good enough with C/Objective-C to write code that works as well as Java?

I'd say, write what you know best. If it doesn't perform, rewrite the critical section, in another language if absolutely needed.

-Lee
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I just wrote a small program in Objective C and in Java to compare relative speeds -- expecting xcode to be much faster, but ....

The xcode took about 5 seconds where the same Java program logic took only 1.4 seconds!

My question is whether using the xcode terminal option and typing in a program that runs within xcode is a fully compiled xcode program?

If running an xcode program in terminal mode is "fully compiled" then I'm confused how xcode could/would be so much slower than the equivalent Java program.

Each program counted from 1 to 2,000,000,000 and did a simple addition inside the loop. Both programs printed out the time before and after the loop.

Look forward to any insights.

Thanks,

-- m

Debug or Release version?

If you don't know, then it is the Debug version and that is of course a lot slower. And I would really like to see the actual code. Just because you think two programs are equivalent doesn't mean they are.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.