Kap*_*ain 1 c java arrays objective-c
"C很快,Java很慢",对吧?
我比较了Video2Brain的Objectiv-C 3.0教程中的时间表示例,a) Eclipse/Java和b) MacBook Pro Quadcore上的XCode/Objectiv-C.结果绝对让我感到惊讶.Java比Objective-C实现快3倍.Java:0.718s与Objective-C相比:2.416s.
问题:我错过了什么?怎么解释这个?谢谢!
a)Java代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
b)Objective-C代码
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);
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
@nhahtdh编译器选项:编译器:Apple LLVM编译器4.2
@jlordo:我得到相同的结果,当我改为timesTable [row] [column] =(row +1)*(column +1)*count
@uchuugaka:是的,我同意它基本上与Java相比较.我两次都进行了五次测试.系统重新启动.没有其他应用程序运行
@Anoop Vaidya:是的,最新版本,我得到相同的时间,没有不同的LLVM GCC 4.2或Apple LLVM编译器4.2
UPDATE2
@justin:谢谢,问题解决了.构建设置 - >优化级别
jus*_*tin 10
你的"基准"存在缺陷.C优化器(用于ObjC测试)应该意识到可以移除整个循环.应在启用优化的情况下执行速度比较(例如-O3).在启用优化的情况下运行测试时,上述程序所用的时间为0.0秒(是的,我测试并确认).
必须设计出更好的基准.
| 归档时间: |
|
| 查看次数: |
1029 次 |
| 最近记录: |