"C is fast, Java is slow", right?
I compared the times table example from Video2Brain's Objectiv-C 3.0 tutorial for a) Eclipse/Java and b) XCode/Objectiv-C on a MacBook Pro Quadcore. The result absolutely surprised me. Java is 3 times faster than the Objective-C implementation. Java: 0.718s compared to Objective-C: 2.416s.
Question: What do I miss? How can this be explained? Thanks!
a) Java Code:
public static void main(String[] args) {
int timesTable[][] = new int[10][10];
long beginTime = System.currentTimeMillis();
// 10 000 000 x
for (int count = 0; count < 10000000; count++) {
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
timesTable[row][col] = (row +1) * (col +1);
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("Time elapsed = " + (endTime - beginTime) + "ms");
}
b) Objective-C Code
int main(int argc, char* argv[]) {
int timesTable[10][10];
CFAbsoluteTime beginTime = CFAbsoluteTimeGetCurrent();
// 10 000 000 x
for (int count = 0; count < 10000000; count++) {
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
timesTable[row][col] = (row +1) * (col +1);
}
}
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@"Time elapsed = %f", endTime - beginTime);
}
UPDATE
@nhahtdh Compiler Options: Compiler: Apple LLVM compiler 4.2
@jlordo: I get same result, when I change to timesTable[row][column] = (row +1) * (column +1) * count
@uchuugaka: Right, I agree its basicly C compared to Java. I run the test five times both. System was restarted. No other applications running.
@Anoop Vaidya: Yes, newest version, I get the same times, no differ LLVM GCC 4.2 or Apple LLVM compiler 4.2
UPDATE2
@justin: Thanks, problem solved. Build Settings -> Optimization Level