Speed comparison

Discussion in 'Mac Programming' started by Mork, Nov 22, 2011.

  1. Mork macrumors 6502

    Joined:
    Jan 9, 2009
    #1
    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
     
  2. willieva, Nov 22, 2011
    Last edited by a moderator: Nov 22, 2011

    willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #2
    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.
     
  3. exabytes18 macrumors 6502

    Joined:
    Jun 14, 2006
    Location:
    Suburb of Chicago
    #3
    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.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    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.
     

Share This Page